开发者

iphone - trying to understand @property

Suppose I have two classes. In the first one I declare this in Class1.h

@interface Class1 : UIViewController {

  NSString *myString;
  id myObject;

}

On the second class I go beyond that I declare it like

@interface Class2 : UIViewController {

  NSString 开发者_StackOverflow中文版*myString;
  id myObject;

}

@property (nonatomic, retain) NSString *myString;
@property (nonatomic, retain) id myObject;

and then I @synthesize myString, myObject on Class2.m

Then, on my main program, I create two objects: one based on Class1 and another one based on Class2.

What effect the @property of class2 will have? Will every value assigned to both values on Class2 be always retained? If so, do I need to "release" them? How?

Thanks.


Please read Declared Properties section of The Objective-C programming language for a full explanation on properties ;)

In Class2:

In this case you set retain attribute to your property it is supposed to be retained in the implementation. This is done automatically when you synthesize a property. This means that you should have

- (void) dealloc{
    [myString release];
    [myObject release];
    [super dealloc];
}

and everything should be fine

In Class1, you don't have properties so myString and myObject is not visible from outside. But this does not mean that you shouldn't release them. It depends on the way you initialize them and/or if you send retain messages to them.

BTW, if you set assign a property you don't release it, just set it to nil in the dealloc method. If you set copy to it then you must release it.

EDIT

You said: *But suppose I have this *

@property (nonatomic, retain) UIView *myView; 

and

myView = [[UIView alloc] initWithFrame:myFrame]; 
[self.view addSubview:myView]; 
[myView release];

? I am already releasing myView... do I have to release it again???

First, since you have your property defined that way, you should have dealloc method as:

- (void) dealloc{
    [myView release];
    [super dealloc];
}

So, the answer is NO you should not release it but actually is not correct. Please take a look:

myView = [[UIView alloc] initWithFrame:myFrame];  //myView retainCount is 1
[self.view addSubview:myView];  //retainCount is 2
[myView release]; //retainCount is 1 again

later in dealloc method

- (void) dealloc{
    [myView release]; // retainCount becomes 0, is deallocated
    [super dealloc]; // subviews of self.view are released but myView was already deallocated!, so you have over released myView once ;(
}

This is the correct way: (Use your properties ;) )

UIView *aView = [[UIView alloc] initWithFrame:myFrame]; // init, retainCount is 1
self.myView = aView; // retainCount becomes 2
[aView release]; // retainCount becomes 1 again and we are fine.

[self.view addSubview:self.myView]; //retainCounts becomes 2 again.

even if it is 2 there is no problem because when self.view is deallocated its subviews also will be released. Hence self.myView retainCount will become 1 again later when self is deallocated.

- (void) dealloc{
    [myView release]; //retainCounts becomes 1
    [super dealloc];  // all its subviews of self.view are released hence myView retaincount becomes 1 and is released corretly
}

What is the difference?

Suppose self.myView is also retained by other object X and with the former approach, X's view will be pointing to an invalid object, because it was already released.

Hope it helps

EDIT2 As bbum's indication, this is a mini-mini-short tutorial on properties:

when you have

@property (... retain) NSObject *retainVar;
@property (... assign) NSObject *assignVar;
@property (... copy) NSObject *copyVar;

and you @synthesize them

is like having the following setters:

// retain
-(void)setRetainVar:(NSObject *)var {
     if (retainVar != var) {
        [retainVar release];
        retainVar = [var retain];
    }
}
//assign
-(void)setAssignVar:(NSObject *)var {
     assignVar = var;
}
//copy
-(void)setCopyVar:(NSObject *)var {
    if (copyVar != var) {
        [copyVar release];
        copyVar = [var copy];
    }
}

(this means that if you assign directly an object you have to make sure is something equivalent to above setters, from the memory management point of view)

and your dealloc method should be something like:

- (void) dealloc{
    [retainVar release];
    assignVar = nil;
    [copyVar release];
    [super dealloc];
}

When setting your ivars

for example, inside of init:

- (id) init{
    if ((self = [super init])){

        //this is ok
        retainVar = [[NSObject alloc] init];//but is retainVar was not nil we will have a leak ;(

        //This is better
        NSObject *obj = [NSObject alloc] init];
        self.retainVar = obj;
        [obj release]; 

        //this is BAD
        assignVar = [[NSObject alloc] init];//because this is like retaining it, later it will leak

        //below is correct
        NSObject *obj = [[[NSObject alloc] init] autorelease];
        assignVar = obj;

        //copy is pretty much like retain,
        //this is ok
        copyVar = [[NSObject alloc] init]; //but, if copyVar was not nil is a leak!

        //below is better
        NSObject *obj = [NSObject alloc] init]:
        self.retainVar = obj;
        [obj release];

     }
     return self;
}


Apple's "Learning Objective C - A Primer" tells you about that and more:

http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜