来自不同类的NSMutableArray获取Null(NSMutableArray from different class gets Null)

编程入门 行业动态 更新时间:2024-10-26 18:18:41
来自不同类的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; @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);

最满意答案

如果其他内容具有对同一属性的强引用,则弱属性引用很有用。 在你的情况下,似乎并非如此。 使您的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.)

更多推荐

本文发布于:2023-07-22 18:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:同类   NSMutableArray   class   Null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!