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

ace2600

macrumors member
Original poster
Mar 16, 2008
71
0
Austin, Texas
Hi,

Given something like
PHP:
@interface ExampleClass : NSObject {
    NSObject *prop;
}

@property (nonatomic,retain) NSObject *prop;
If I access prop within ExampleClass am I accessing it as a property (using the property getters and setters) or am I accessing the variable directly? If directly, do I need to call self.prop to access using the property getters/setters/etc.?

Also, does anyone know of links to good examples on retain vs assign. I've read a few articles, but I feel example situations on when you'd use one over the other would be helpful.
 

tacoman667

macrumors regular
Mar 27, 2008
143
0
Hi,

Given something like
PHP:
@interface ExampleClass : NSObject {
    NSObject *prop;
}

@property (nonatomic,retain) NSObject *prop;
If I access prop within ExampleClass am I accessing it as a property (using the property getters and setters) or am I accessing the variable directly? If directly, do I need to call self.prop to access using the property getters/setters/etc.?

Also, does anyone know of links to good examples on retain vs assign. I've read a few articles, but I feel example situations on when you'd use one over the other would be helpful.

In my book it says that in Obj-C 2.0 garbage collection, retain and assign have no real difference because the GC will pick em up automatically. If I recall though.

The book also says that if you are using the property to instantiate auto getters/setters then you would need to use "dot" notation, for eaxample use self.property over [property doesSomething].
 

therevolution

macrumors 6502
May 12, 2003
468
0
If I access prop within ExampleClass am I accessing it as a property (using the property getters and setters) or am I accessing the variable directly? If directly, do I need to call self.prop to access using the property getters/setters/etc.?

Yes, access to prop would be direct. self.prop would use the getter/setter methods.

Also, does anyone know of links to good examples on retain vs assign. I've read a few articles, but I feel example situations on when you'd use one over the other would be helpful.

I don't know of any resources, no. But if you understand what retain means, and where you should and shouldn't use it, it's not too hard. If you don't know how/when to use retain, definitely learn it ASAP. It's pretty fundamental.

I would say as a rule of thumb, you're going to want to retain anything that inherits from NSObject. You'd use assign for primitives, like int, float, BOOL, and also for things like structs.
 

ace2600

macrumors member
Original poster
Mar 16, 2008
71
0
Austin, Texas
Thanks for the help!

self.prop = nil has the same affect as [prop release]? If so, is there a preference on the best way to deallocate the variable?
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
Thanks for the help!

self.prop = nil has the same affect as [prop release]? If so, is there a preference on the best way to deallocate the variable?

Interesting question :) I would try it and see. Something like

Code:
NSLog(@"Retain count is: %d", [self.prop retainCount]);
self.prop = nil;
NSLog(@"Retain count is: %d", [self.prop retainCount]);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.