Instantiating objects with variable names

Discussion in 'Mac Programming' started by tw002, Jan 10, 2010.

  1. tw002 macrumors newbie

    Joined:
    Feb 23, 2009
    #1
    Hi.

    I'm in the process of (trying) to learn Obj C/Cocoa, and I'm all together new to OOP.

    I'm trying to write a simple app that will store to do's. At the moment, all I want it to do is to read data from input text fields (working) and on the press of the create button, instantiate a new todoItem object, with the data stored as instance variables. So essentially every to do item is an object.

    My problem is, this all works, but only when there's just one object being instantiated.

    For example:
    When the 'create to do' button is pressed, an IBAction method runs this code:

    Code:
    todoItem *itemOne = [todoItem alloc] init];
    then writes its ivars etc..

    Obviously, if you press the button again, the exact same code is run, giving an error 'redefinition of itemOne'. Rather than doing this, I need for the object to be dynamically named, so each time the button is pressed objects are instantiated and named automatically eg. itemOne, itemTwo etc.

    Is this possible? Perhaps an array of objects?

    I hope you understood that longwinded description.

    Thanks in advance, Thomas.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    This is not possible.

    This is. And is sensible.

    Due to you even asking this I have to say it appears you have no basic programming skills. Stop coding: go away and learn some basics.
     
  3. tw002 thread starter macrumors newbie

    Joined:
    Feb 23, 2009
    #3
    Hence the 'in the progress of learning'. Thanks anyway.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Then you need to learn the theory: basic statements then loops and conditionals then data structures. Once you know those in abstract terms start looking at Objective-C/Cocoa specific examples of them.

    Trying to learn by coding before doing the above almost always ends in failure.
     

Share This Page