How Do I create an array?

Discussion in 'iPhone/iPad Programming' started by iphonedevelop18, Apr 6, 2009.

  1. macrumors member

    Joined:
    Feb 26, 2009
    #1
    I've tried to search through these and other forums but i cannot figure out how to create an array.
     
  2. macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #2
    []
     
  3. macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #3
    You can create an array of objects by using NSArray or NSMutableArray.
     
  4. thread starter macrumors member

    Joined:
    Feb 26, 2009
    #4
    i know that but how do i set it up?
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What do you mean "set it up"? Define it? Populate it? What?
     
  6. thread starter macrumors member

    Joined:
    Feb 26, 2009
    #6
    I mean put objects into the array to be accessed later, like numbers or strings.
     
  7. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #7
    In the documentation for NSArray, look under "Tasks" -> "Creating an array"
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    You've looked through the NSArray Class Reference, right? Is there something in particular you don't understand? Rather than give you the outright answer (which would be very easy), we're trying to guide you to educate yourself, a very handy skill to have!
     
  9. thread starter macrumors member

    Joined:
    Feb 26, 2009
    #9
    After looking a bit more I found this piece of code.

    Code:
     NSArray *array = [NSArray arrayWithObjects: @"John", @"Bob", [NSNull null], @"Jane"];
     
    Now my main question is where do I declare this and also how do I put an NSNumber into an array like this one which just uses strings?
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Where do you need to access this array?

    The NSNull usage in that declaration should give you a hint.
     
  11. thread starter macrumors member

    Joined:
    Feb 26, 2009
    #11
    in the .m viewController

    So like this?

    Code:
    NSNumber *N1 = [numberWithInt:1]; 
    NSNumber *N2 = [numberWithInt:2];
    NSArray *Myarray = [NSArray initWithObjects:N1,N2,nil];  
    
     
  12. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    In multiple methods?

    Close. Your previous example used arrayWithObjects: Do you understand why? You can use initWithObjects: if you like, but there will be something else you'll need to do to your array first. Do you know what it is?
     
  13. thread starter macrumors member

    Joined:
    Feb 26, 2009
    #13
    No it's just going to be used in one view controller

    Do I need to allocate it?
     
  14. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Yes, but a single view controller can contains multiple methods. If you only need access to it from within one method, declare and init your array in that method. If you need access to it from multiple methods, either declare it as a static or an instance variable, accordingly.

    Ding Ding Ding! We have a winner! Good job!
     
  15. thread starter macrumors member

    Joined:
    Feb 26, 2009
    #15
    Thank you so much for helping me through this one rather than telling me the answer, really helped alot!
     
  16. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    You're welcome. Appreciate the feedback.
     

Share This Page