Variable that retains its value?

Discussion in 'iOS Programming' started by Danneman101, May 10, 2009.

  1. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    Ive got a project with a NSArray which Ive declared and set property in the .h-file like this:

    Code:
    @interface LeafViewController : UIViewController <UIWebViewDelegate, AVAudioPlayerDelegate, UITabBarControllerDelegate>
    {
    	NSArray *buttonNamesSecondRow;
    }
    @property (nonatomic, retain) NSArray *buttonNamesSecondRow;
    
    And in the .m-file Ive synthesized it with:

    Code:
    @synthesize buttonNamesSecondRow;
    
    Now, this should mean that I can use the array anywhere in the .m-file.

    The problem is that if I set its value in one function, and tries to extract its value (that I just set in the first function) from another function, it seems to be empty.

    Code:
    // Setting the arrays content in function 1
    - (void)viewWillAppear:(BOOL)animated
    {
    	buttonNamesSecondRow = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
    }
    
    // Trying to get the arrays content in function 2..
    - (void) segmentAction: (id) sender
    {		
    	// But array is empty!
    	NSString *letter = [buttonNamesSecondRow objectAtIndex:2];
    }
    
    Does it have to do with the way I set the property of the array? Ive used "retain" which I thought should mean that it retains its content.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You should be using the setter method or the dot notation to set the variable otherwise the setter is not called and the variable will not be retained.
     
  3. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #3
    to clarify this a little more:
    varname = something;
    always uses the setter-method "assign"
    self.varname = something;
    uses the setter-method your defined in the @property-statement.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I would disagree with that somewhat. varname = something is an assignment. It does not call any setter method. If you declare your property as assign then a setter method does get generated that will do this, but in this case calling self.varname will still call a method with the associated overhead.
     
  5. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #5
    I knew someone would say that :D yeah, technically it does not use an assign-setter-method, but the end result is the same as if it would. better? :D
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Yes but the difference is important if you subclass the class as you can override the setter method (I think) altering it's behaviour but this would have no effect on varname = value;.
     
  7. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #7
    Ok, so self.variablename = something; will retain the value, and you also need to release it in dealloc later. And using simply variablename = something; will only assign a temporary value. Great, getting the hang of this, I think :D Thanks a lot guys!
     

Share This Page