上面的代码有很多问题。首先,让我指出 if 语句和函数都没有右括号。然后,== YES
在括号之外。接下来,您尝试将 的实例与NSString
布尔值进行比较。最后,既没有defaults
也kMusic
没有被宣布。
所以这里有一些固定的代码:
-(IBAction)press {
cruzia.hidden = 0;
textarea.hidden = 0;
defaults = [NSUserDefaults standardUserDefaults];
//if defaults has been instantiated earlier and it is a class variable, this won't be necessary.
//Otherwise, this is part of the undeclared identifier problem
/*the other part of the undeclared identifier problem is that kMusic was not declared.
I assume you mean an NSString instance with the text "kMusic", which is how I have modified the below code.
If kMusic is the name of an instance of NSString that contains the text for the key, then that is different.
also, the ==YES was outside of the parentheses.
Moving that in the parentheses should fix the expected expression problem*/
if ([defaults boolForKey:@"kMusic"] == YES) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"click", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
}
现在,在您复制并替换旧代码之前,您应该了解我所做的假设。我假设defaults
以前没有被声明和实例化。最后,我假设您正在寻找一个与字符串键“kMusic”一起存储的布尔值,因此在代码的其他地方您使用类似的东西[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"kMusic"];
如果这不是您的想法,您将需要进行更改因此。
最后,下一次,在将代码带到 Stack Overflow 之前,重新阅读您的代码是否存在拼写错误。