Reputation: 4656
I pass the category id to the next view controller.
NSNumber *childCategoryId = [[NSNumber alloc] initWithInt:category.id];
if ([destination respondsToSelector:@selector(setCategoryId:)]) {
[destination setValue:childCategoryId forKey:@"categoryId"];
}
In the next view controller, the property to accept the data is declared as weak
@property (weak, nonatomic) NSNumber *categoryId;
Under viewDidLoad, I use the id to fetch the data from internet. It works fine when the NSNumber is not too big (<15). Therefore, the codes is able to work on the many categories except the those with the id >=15
The problem can be addressed by changing the weak reference into strong reference so the categoryId is kept as long as the next controller wants. I believe this problem is caused by the loss of weak property but i don't get the reason of it.
Upvotes: 2
Views: 694
Reputation: 57050
I assume you are working with ARC.
When you create the NSNumber, it is only retained (by the compiler generated code) in the scope of the method you created it in. When you set it in the weak property, it is not retained by your view controller (due to weak property). At the end of the scope, the object is released (again, by the compiler generated code). The fact that it works for some values and not for other is an internal implementation detail. You should assume it will never work. You should indeed set your property to strong
or copy
.
Upvotes: 1
Reputation: 46543
strong
is used to own the object. It retains the object. Whereas weak
doesn't retain it and it is release
d.
A weak reference you signify that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to nil.
A strong reference "owns" the object you are referencing with this property/variable. The compiler will take care that any object that you assign to this property will not be destroyed as long as you (or any other object) points to it with a strong reference. Only once you set the property to nil will the object get destroyed (unless one or more other objects also hold a strong reference to it).
Upvotes: 4