Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 其中';我的声音怎么样?_Objective C_Cocoa_Nsmutablearray_Nskeyedarchiver - Fatal编程技术网

Objective c 其中';我的声音怎么样?

Objective c 其中';我的声音怎么样?,objective-c,cocoa,nsmutablearray,nskeyedarchiver,Objective C,Cocoa,Nsmutablearray,Nskeyedarchiver,在我的Row类中,我有initWithCoder方法,在该方法中恢复的所有内容都可以正常工作,但只在该方法中。调用该方法后,我释放了Row类中的一组声音。sounds类也有initWithCoder方法,声音播放良好,但仅在行类initWithCoder方法中播放。解码行对象后,声音数组将完全消失,无法调用 以下是我的initWithCoder源代码: - (id) initWithCoder:(NSCoder *)coder { ... soundArray = [coder

在我的Row类中,我有initWithCoder方法,在该方法中恢复的所有内容都可以正常工作,但只在该方法中。调用该方法后,我释放了Row类中的一组声音。sounds类也有initWithCoder方法,声音播放良好,但仅在行类initWithCoder方法中播放。解码行对象后,声音数组将完全消失,无法调用

以下是我的initWithCoder源代码:

- (id) initWithCoder:(NSCoder *)coder {
    ...
    soundArray = [coder decodeObjectForKey:@"soundArray"];
    NSLog(@"%d",[soundArray count]);
    return self;
}
日志将计数显示为应该的8(这是在未归档时)。然后分配我创建的行对象。并且生成的行对象不再具有soundArray

[NSKeyedArchiver archiveRootObject:row toFile:@"DefaultRow"];
...
row = [NSKeyedUnarchiver unarchiveObjectWithFile:@"DefaultRow"];
所以现在每当我调用soundArray时,它就会崩溃

//ERROR IS HERE
NSLog(@"%d",[[row soundArray] count]);

请提供帮助(soundArray是一个NSMutableArray)。

您需要将soundArray保留在
initWithCoder:
-您可以在上阅读有关Cocoa内存管理的内容


如果您不
保留
soundArray,它将在以后被
解除锁定
,因为您尚未声明它的所有权。基本上,
decodeObjectForKey:
返回一个已
自动删除的对象

Dane Man:不要跳过链接到的文档。读一读,然后全部读一遍。这是每个Cocoa和Cocoa Touch程序员的必读资料。