NSArray Problem

Discussion in 'iOS Programming' started by xcodeNewbie, Jul 7, 2011.

  1. xcodeNewbie macrumors member

    Joined:
    Jul 1, 2011
    #1
    I've been having problems with NSArrays. I'm making an app, and part of the interface for the app is a list of options the user can pick. I have a UILabel, and two buttons on top and bottom of it. Clicking the buttons will take you up or down the list of options, which are displayed in the label. All of the options are strings inside an NSArray, and called with objectAtIndex. I set the NSArray in viewDidLoad, but when I click the buttons to scroll the options, the app crashes. However, when I set the Array in the same method that I use for the button clicks, everything works fine. I've fiddled around with this, and noticed an unbroken pattern: When I try to access an NSArray with a method, and that Array was set in a different method, the app crashes. This NSArray is an instance variable for my class, so every method should be able to use it.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Without seeing exactly what your code is, the only thing I can think of is maybe you need to be using an NSMutableArray instead? Or maybe a local variable of the same name is screwing with the instance variable?
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    I think you are trying to reset an NSArray, which is impossible, therefore you have an NSMutableArray like the guy above me pointed out. try that instead.
    because it is MUTABLE.
     
  5. xcodeNewbie thread starter macrumors member

    Joined:
    Jul 1, 2011
    #5
    Thanks, I changed it to NSMutableArray and used @property and @synthesize to set it. Everything works fine now.
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    You don't need the @synthesize if it's for within your own class. because if you are using @synthesize, you are exposing it to other classes. Just saying.
    It's ok to just do
    NSMutableArray *nameArray;

    And set it in your own implementation, without making it a property.
    (If i'm not mistaking, otherwise RD, will explain you better.)
     

Share This Page