请问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;
}
mutableArray在用Array赋值时不能直接 = ,这样mutableArray实际上指向的是NSArray,导致后续错误。一般采用nsmutableArray = [[nsmutableArray alloc] initwithArray:array]的形式。