开发者社区> 问答> 正文

iOS nsmutalblearray addobject程序崩溃

请问NSMutableArray在alloc init之后再调用addobject还是会崩溃的可能原因有哪些?
出现错误:[__NSArrayI addObject:]: unrecognized selector sent to instance 0xb848630'

ChatMessage *newMsg = [[ChatMessage alloc] init];
newMsg.owner = MSG_OWNER_SELF;
newMsg.type = MSG_TYPE_SOUND;
newMsg.timeStamp = self.dateToRecordStr;
newMsg.msg = [NSString stringWithFormat:@"%@%@.aac", self.toChatUsr.UID, newMsg.timeStamp];
[self.toChatUsr.msgs addObject:newMsg];

这是代码
最后一句会崩掉。
toChatUsr是 @property(weak, nonatomic),赋值是 = 一个@property(strong, nonatomic)

chatMsssage的初始化代码

@implementation ChatMessage

(id)init{
self = [super init];
if (self) {
self.owner = [[NSString alloc] init];
self.type = [[NSString alloc] init];
self.timeStamp = [[NSString alloc] init];
self.msg = [[NSString alloc] init];
}
return self;
}

展开
收起
a123456678 2016-07-27 16:42:56 2542 0
1 条回答
写回答
取消 提交回答
  • mutableArray在用Array赋值时不能直接 = ,这样mutableArray实际上指向的是NSArray,导致后续错误。一般采用nsmutableArray = [[nsmutableArray alloc] initwithArray:array]的形式。

    2019-07-17 20:00:50
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载