gpt4 book ai didi

objective-c - 我的声音去哪儿了?

转载 作者:行者123 更新时间:2023-12-03 17:44:51 25 4
gpt4 key购买 nike

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

这是我的 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)。

最佳答案

您需要在 initWithCoder: 中保留 soundArray — 您可以在 Apple's site 上阅读有关 Cocoa 内存管理的内容。 .

如果您不retain soundArray,它将在稍后被dealloc'ed,因为您尚未声明其所有权。基本上,decodeObjectForKey: 返回一个已自动释放的对象。

关于objective-c - 我的声音去哪儿了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3042089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com