[英]Error ARC 'release' is unavailable error becomes "expression result unused'
I know that this is a similar question to the ARC deprecated code however, I have some old code from NIB days that I am upgrading to Storyboards and in doing so, the code revisions are causing me some issues. 我知道这是与ARC不推荐使用的代码类似的问题,但是,在NIB时代,我有一些旧代码正在升级到Storyboard,因此,代码修订引起了我一些问题。
In the code below: 在下面的代码中:
- (void)audioSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound-file" ofType:@"mp3"];
if(theAudioSound)[theAudioSound release];
theAudioSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudioSound.volume = 0.30;
[theAudioSound play];
}
I get the ARC problem; 我遇到了ARC问题; After many attempts to find a solution I can only find the following advice.
经过多次尝试找到解决方案后,我只能找到以下建议。
Delete the 'release'
If I do delete the release - depending on how I delete it - I get the following error messages. 如果确实删除了发行版(具体取决于删除方式),则会收到以下错误消息。
from 从
if(theAudioSound)[theAudioSound release];
to 至
if(theAudioSound)[theAudioSound];
the Error is "Expected Identifier" 错误是“期望的标识符”
and from 和从
if(theAudioSound)[theAudioSound];
to 至
if(theAudioSound)theAudioSound;
the Warning is "Expression Result Unused" 警告是“未使用表达结果”
if I use the second of the two changes will it work? 如果我使用这两个更改中的第二个,它将起作用吗? and if not;
如果不; what would I need to do to change the code so that it does work?
我需要做些什么来更改代码以使其起作用?
// if(theAudioSound)[theAudioSound release];
You need to totally comment out that line for this to work. 您需要完全注释掉该行才能起作用。
Actually this is another way of writing following 其实这是另一种写法
if(theAudioSound)
{
[theAudioSound release];
}
If you simply delete release
, that would generate a syntax error. 如果仅删除
release
,则将生成语法错误。
"Delete the 'release'" means you don't need to release it. “删除'发行版'”意味着您不需要发行它。 So, you just remove the line or comment in. That's just all I think.
因此,您只需要删除行或添加注释即可。这就是我的全部想法。
- (void)audioSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound-file" ofType:@"mp3"];
//if(theAudioSound)[theAudioSound release];
theAudioSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudioSound.volume = 0.30;
[theAudioSound play];
}
Well, release
actually releases( decreases the retain count) here, if any object has already has been created. 好吧,如果已经创建了任何对象,那么
release
实际上会在此处释放(减少保留计数)。 But in ARC, you don't have to release
it manually. 但是在ARC中,您不必手动
release
它。
I suggest clearing the AVAudioPlayer
instance in stead of release
, and it will work. 我建议清除
AVAudioPlayer
实例而不是release
,它将起作用。 So the code will look like: 因此代码如下所示:
if(theAudioSound) {
theAudioSound = nil;
}
OR 要么
Simply delete or comment out the line if(theAudioSound)[theAudioSound release];
只需删除或注释掉
if(theAudioSound)[theAudioSound release];
like: 喜欢:
// if(theAudioSound)[theAudioSound release];
And continue with the rest of codes. 并继续其余的代码。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.