开发者

NSNumber not being retained?

开发者 https://www.devze.com 2023-02-21 23:15 出处:网络
I have a variable lastPostsGrabbedCounter, an NSNumber, that is defined below. .h NSNumber *lastPostsGrabbedCounter;

I have a variable lastPostsGrabbedCounter, an NSNumber, that is defined below.

.h
NSNumber *lastPostsGrabbedCounter;
@property (nonatomic, retain) NSNumber *lastPostsGrabbedCounter;

.m
@synthesize postDetailViewController, lastPostsGrabbedCounter;

- (void)viewWillAppear:(BOOL)animated {
    self.lastKnownLocation = [[CLLocation alloc] init];
    self.lastPostsGrabbedCounter = [[NSNumber alloc] initWithInt:25];
 [self showActivityViewer];

}

This .m file is a table controller in my main view. When the app is loaded this viewWillAppear gets called, but if I navigate to an开发者_如何转开发other TAB and come back and I try to use the lastPostsGrabbedCounter var, I it shows it as nil?

Why isn't it retained when I navigate away?


If the NSNumber was not being retained, then your app would (most likely) crash or, at least, behave badly. That you are seeing a nil indicates that there is a very different problem occurring.

Check to make sure you aren't nilling it out on some other code path. Also, make sure that you are coming back to the instance you think you are. A handful of NSLog(@"%@ %p", [self class], self]); spread throughout the methods can be very helpful.

And as André said, you are leaking the number; over-retaining it.

0

精彩评论

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