Re: Objective 2.0 properties
Re: Objective 2.0 properties
- Subject: Re: Objective 2.0 properties
- From: Ken Thomases <email@hidden>
- Date: Thu, 16 Oct 2008 22:09:44 -0500
Let's start at the end, where you asked:
On Oct 16, 2008, at 9:19 PM, Ignacio Enriquez wrote:
So I would like to know the difference between self.property and
just property
self.property used for reading the property value (as opposed to
setting it) is exactly equivalent to [self property]. That is, an
invocation of the getter for the property.
Let's thinks the next case :
@interface Lesson : NSObject {
NSString *lessonTitle;
NSDate *referDate;
NSNumber *lessonDuration;
}
@property (nonatomic, retain) NSString* lessonTitle;
@property (nonatomic, retain) NSDate *referDate;
@property (assign) NSNumber * lessonDuration;
Using "assign" here is almost certainly not what you want. An
NSNumber* is an object pointer. If you don't retain it, then you're
failing to ensure that the object lives as long as your reference to it.
@property (readonly) UIImage * lessonImage;
@end
@implemetantion Lesson
@synthesize lessonTitle;
@synthesize referDate;
@synthesize lessonDuration;
-(UIImage *) lessonImage{
return [UIImage imageNamed:
[NSString stringWithFormat:@"cellImage%@",self.lessonTitle]]; //is
self. ok?
}
If you're using KVO or Bindings on the lessonImage property, make sure
to tell KVO about its dependency on the lessonTitle property.
+keyPathsForValuesAffecting<Key>
@end
So the question is
Inside Lesson.m when i want to call or refer lessonTitle what is the
best?
self.lessonTitle or just only lessonTitle? (note that is nonatomic,
retain))
Either may be used. The former calls the getter for the property, the
latter accesses the instance variable directly. In this case, we know
from what you've shown use that the getter for lessonTitle has no
special side effects that you'd either want to be sure took effect or
want to be sure to avoid.
Direct access to the instance variable is slightly faster, but that
should be that last of your concerns.
the same
Inside Lesson.m when I want to call or refer lessonDuration, What is
the best? self.lessonDuration or just lessonDuration (note that is
assign)
Same as above.
the same
Inside Lesson.m when I want to call or refer lessonImage what is the
best?
self.lessonImage or just lessonImage? (note that is readonly)
"Just lessonImage" surely won't compile. There is no instance
variable named lessonImage. A naked reference to "lessonImage" is an
attempt to access a variable, in this context usually an instance
variable (it might also be a local variable or a global variable).
I have been trying a lot of conbination and none of them result in
compilation errors,
Even the naked "lessonImage"?!?
but when I run the application it crashes.!
Where does it crash? What is the nature of the crash?
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden