开发者

Retain on an retained variable from another object?

I have a UIViewController (call it A) with a NSDate instance I'm already retaining and synthesize.

In another method I am creating another UIViewController (call it B) and wish to pass it the NSDate in开发者_StackOverflow社区stance. In B, I am going to be using it. Should I be also be retaining and synthesizing this instance as well? I assume B will be using the same NSDate instance that A created, and there might be the chance that A's instance could be deleted in viewDidUnload if memory is running out.

On the other hand, if I retain and synthesize it, I'm going to deallocate it when B's view is gone. And when I return to A, the NSDate instance is gone.

So, what is the best practice here? Should I just create a new NSDate object in B and make it the same date as the one passed in? and retain/synthesize this new instance?


I could be misunderstanding your question, but it doesn't sound like you have a very good idea of how reference counting works in Objective C. (I don't mean any offence by that; it can be tricky to get the hang of.) If you're managing your memory properly, your NSDate instance will not be deallocated when B's view is gone. Your setup should be something like this.

// A.h
@property(nonatomic, retain) NSDate *myDate;
@synthesize myDate;

// A.m
- (id) init
{
    if ( self = [super init] )
    {
        // Setter retains the new NSDate instance; its reference count is now 1
        // (after autorelease)
        [self setMyDate:[NSDate date]];
    }

 return self;
}

- (void) someMethod
{
    // Assume b is a pointer to your B instance. B's setter retains the NSDate
    // again; it's reference count is now 2
    [b setMyDate: myDate];
}

- (void) dealloc
{
    [myDate release];
}

// B.h
@property(nonatomic, retain) NSDate *myDate;
@synthesize myDate;

// B.m
- (void) dealloc
{
    // Assuming B is deallocated first, the NSDate's reference count will
    // now be 1.  It will still be accessible to the A.
    [myDate release];
}

Now, when your B instance is deallocated, your NSDate instance will be released. Being released is not the same thing as being deallocated. The released object will still have a reference count of 1 because it was originally retained by the A instance. The NSDate instance will not be deallocated until the A instance releases it and its reference count drops to zero. Hope that helps.

More information on memory management in Objective C is available here. See also this question.


I totally understand what you want. I suggest you to make property for NSDate.

//.h

NSDate *date;  

@property(nonatomic,retain) NSDate *date;  

//.m  

@synthesize date; 

And use date in B viewcontroller. Now the operations you are carrying out initially on date should not be in viewDidLoad, they should be in viewWillAppear. This will help you get date value when you return from B to A as viewWillAppear is always called.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜