PDA

View Full Version : Silly property question




ace2600
Jun 12, 2008, 11:03 PM
Hi,

Given something like @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
Jun 12, 2008, 11:22 PM
Hi,

Given something like @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
Jun 12, 2008, 11:24 PM
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.

tacoman667
Jun 12, 2008, 11:36 PM
(...) retain anything that inherits from NSObject (...) You'd use assign for primitives, like int, float, BOOL, and also for things like structs.

*SMACKHEAD* Duh. I forgot about that till I read your post.

ace2600
Jun 14, 2008, 08:53 PM
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
Jun 14, 2008, 09:12 PM
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


NSLog(@"Retain count is: %d", [self.prop retainCount]);
self.prop = nil;
NSLog(@"Retain count is: %d", [self.prop retainCount]);