0

I've this class:

@interface PersonModel : NSObject

@property (nonatomic, weak) NSString *string;
@property (nonatomic, weak) NSMutableArray *array;

@end

and in another class, I use that string and array. The string goes fine, but the array getting null. I initiate it as usual, like this:

person.array = [[NSMutableArray alloc] init];
[person.array addObject:[object copy]];
NSLog(@"Array: %@", person.array);
1
  • Did you use that array in your first class If yes then Did you get something in that array Commented Dec 21, 2012 at 12:33

2 Answers 2

7

A weak property reference is useful if something else has a strong reference to the same property. In your case it doesn't seem like that's the case. Make your Array into a strong property.

(For ease of reading, don't name your variables starting with capital letters; array is slightly better than Array. Something meaningful would be better still.)

Sign up to request clarification or add additional context in comments.

6 Comments

I see what you mean (if you look below at my answer, I wasn't sure why setting it to a local variable first fixes it, but now I understand). Good answer.
@groomsy -- Note though that with the local variable method, once you drop out of that variable's scope the weak property is subject to the same (null) behavior.
You're correct. I was aware of that. This would necessitate a strong property if you need it out of that scope. I was aware of that aspect. However, I didn't realize that the weak property wouldn't hold that reference when assigned within that scope. For whatever reason, I thought assigning the instance directly to the weak property would retain it through our method scope. That's the part that tripped me up. After your explanation though, it seems obvious that it wouldn't.
I just thought of one other thing to go along with this discussion: Essentially, the scope for the new instance of NSMutableArray is the setter method. Since nothing retains it in this set method, the memory isn't set anymore. In my example, the local variable means that the scope becomes the method all of that was called in. This is the realization I just had. Thank you Phillip Mills for sparking an interesting discussion/brain exercise for me this morning. :D
The Array is just un example name. Since I'm working on a client project, I altered the variable names in the question :). Turn it into a strong variable worked for me. Thanks a lot! I had turned it into weak, because I'm having a lot memory problems. I've to send a lot of information to a nodejs server, and after 500 arrays with 50 fields for each array, the app crashes.
|
1

This code appears okay. I was able to get it to work by making the NSMutableArray a strong property instead of a weak property. I'm still testing to figure out why this change in code makes a difference.

EDIT: I've also noticed that if you first set the new NSMutableArray instance to a variable, it works with a weak property type:

PersonModel *personModel = [[PersonModel alloc] init];
NSMutableArray *tempMutableArray = [[NSMutableArray alloc] init];
[personModel setArray:tempMutableArray];
[personModel.array addObject:[someString copy]];
NSLog(@"Array: %@", personModel.array);

EDIT 2: This works because your weak reference needs at least one strong reference (see Phillip Mills's response). It's good to know why setting the new instance of NSMutableArray to a temporary variable worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.