来自不同类的NSMutableArray获取Null(NSMutableArray from different class gets Null)
我上过这堂课:
@interface PersonModel : NSObject @property (nonatomic, weak) NSString *string; @property (nonatomic, weak) NSMutableArray *array; @end在另一个类中,我使用该字符串和数组。 字符串很好,但数组变为null。 我像往常一样启动它,像这样:
person.array = [[NSMutableArray alloc] init]; [person.array addObject:[object copy]]; NSLog(@"Array: %@", person.array);I've this class:
@interface PersonModel : NSObject @property (nonatomic, weak) NSString *string; @property (nonatomic, weak) NSMutableArray *array; @endand 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);最满意答案
如果其他内容具有对同一属性的强引用,则弱属性引用很有用。 在你的情况下,似乎并非如此。 使您的Array成为一个strong属性。
(为了便于阅读,不要以大写字母开头命名变量; array稍微好于Array 。有意义的东西会更好。)
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.)
更多推荐
发布评论