Re: Objective 2.0 properties
Re: Objective 2.0 properties
- Subject: Re: Objective 2.0 properties
- From: "Ignacio Enriquez" <email@hidden>
- Date: Sat, 18 Oct 2008 02:25:12 +0900
Thanks for your responses.
Chuck:
First : I used "assign" in order to be taught about it, actually I
have never explicitly used. but As far as I know assign is the default
so implicitly I have used it. with out problems.
Second: Let me see If I understood. they should be copy since all the
classes all conform to NSCopying. this means that all (and I mean ALL
) properties should be "copy"?? (since all objects inherits from
NSObject and this class conform to NSCopying!?) I Think I missing
something here.
Third: I would like to use accessor methods also, but I have too many
lines of code to change. So is still easy to learn about properties.
lol.
and Fourth: I have read the documentation and there is no a good
example for "@property (nonatomic, retain) myproperty " case
which is the one I am interested the most.
Ken:
First: self.property is only or reading right? this also means that
for setting the value I should only use property (and not
self.property ?)
Does this applies also for "retain" attribute?
I am not very sure about this because this is one cause of my crashes.
In first place I wrote a simple class , but the fact is that I wrote a
singletton inside my program .
and all of my properties are "nonatomic, retain"
why nonatomic?? -> nonatomic is faster than the default atomic
why retain? -> I thought that retaining every property I would not
have problems with object life (retainCount etc... )(but I might be
wrong about this because of my crashes)
Well the thing is : I didn't write
self.property = myValue
but only
property = myValue;
in 99% of the cases.
and when I wanted to refer (or use) this class instance's properties
from another class, it results that there is nothing so the app
crashes. In others words is like the object is not longer living any
more.
then I added "self." before every property and worked fine. But I
don't know the reason.
Not my program is getting bigger and bigger and having more often
problem with this.
I would like to know if there is a general rule about properties usage,
for example
when to use retain? when to use assign? when to use copy?
when you use "retain" use "self.property" for... because ...and use
only "property" for... because....
when you use "assign" use "self.property" for...because ... and use
only "property" for ... because...
(same for "copy", "self.property" and "property")
Does any one knows this gold rule? ; )
2008/10/17 Charles Steinman <email@hidden>:
> --- On Thu, 10/16/08, Ignacio Enriquez <email@hidden> wrote:
>
>> @interface Lesson : NSObject {
>> NSString *lessonTitle;
>> NSDate *referDate;
>> NSNumber *lessonDuration;
>> }
>> @property (nonatomic, retain) NSString* lessonTitle;
>> @property (nonatomic, retain) NSDate *referDate;
>> @property (assign) NSNumber * lessonDuration;
>
> Why is lessonDuration assign? That's begging for a crash. All three of these should be copy since the classes all conform to NSCopying.
>
>> 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))
>
> I prefer to always use accessors. It simplifies memory management if nothing else.
>
>> I have been trying a lot of conbination and none of them
>> result in
>> compilation errors, but when I run the application it
>> crashes.!
>> So I would like to know the difference between
>> self.property and just property
>
> http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_2_section_3.html#//apple_ref/doc/uid/TP30001163-CH11-SW17
>
> Cheers,
> Chuck
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
>
> 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