Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.

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.
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
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.

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
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.