converting Java to Objective-C

Discussion in 'Mac Programming' started by cs.iris, Jan 17, 2012.

  1. cs.iris, Jan 17, 2012
    Last edited by a moderator: Jan 17, 2012

    cs.iris macrumors newbie

    Jan 11, 2012
    Hello :)
    I am a student that is currently converting a java program into Objective-C. It was kinda hard, but I need to do it to pass my subject.;) I have a little problem regarding the ".add" of Java. I read a lot of posts and books just to understand it. But I still have a hard time understanding.:confused: I would like to check if what I did was correct and if someone can lend me a hand in converting. Help please? thanks.:)

    Here is the java code:
    ArrayList cid = new ArrayList();
    ArrayList sid = new ArrayList();
    ArrayList<String> sol = new ArrayList<String>();
    public mainProgram() {
    initComponents(); //.add (cartesian product principle)
    cid.add("1 /x");
    here is my solution in objective-c:
    NSArray *cid = [[NSArray alloc]init];
    NSArray *sid = [[NSArray alloc]init];
    NSMutableArray *sol = [[NSMutableArray array];
    (my first trial in .add) cid addObject: @"1/x"; -----but it shows an error: "Expected ';' before 'addObject'"
    (the other trial is) [sid addObject: @"sink"]; -------but it also shows an error:
    "Local declaration of sid hides instance variable"
    "NSArray may not respond to '-addObject'."
  2. jiminaus macrumors 65816


    Dec 16, 2010
    We will not do homework/assignments for you. So don't expect anyone to give you an explicit, straight-up answer here. However do expect people to help you understand enough for you to be able to do it yourself.

    Let's start of with two thing.

    Firstly, what the difference between NSArray and NSMutableArray? If you don't know, check out the class references at and

    If you're going to try to add an object to an array, which do you think you'll need? What type of objects are sid and cid pointing to? What should they be? How do get them to be what they should be?

    Second how do you send a message (call a method) of an object in Objective-C? What's the syntax? How did you violate the syntax in your first attempt?

    There's more wrong with your code than this, but let's start with this.
  3. DESNOS macrumors 6502

    Aug 24, 2011
    First of all, an NSMutableArray is an array which CAN be modified. Most classes are unmodifiable by default, but have a "Mutable" version which can be modified. Also, you have some serious syntax problems. Remember that any calls to a class must be wrapped in square brackets (Objective-C 2.0 changes this a bit, but we'll pretend that never happened for now). I really don't think you worked too hard on this before asking us...
  4. cs.iris thread starter macrumors newbie

    Jan 11, 2012
    I'm sorry if you feel that way, though I don't intend for you guys to do it for me. Thanks for the reply, it was helpful in some ways to understand what I should do. And I learned that there were really a lot more errors in the syntax. I've fixed it already. Thanks :)

Share This Page