Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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
Iphone 使用带有默认值的If语句?_Iphone_Objective C_If Statement - Fatal编程技术网

Iphone 使用带有默认值的If语句?

Iphone 使用带有默认值的If语句?,iphone,objective-c,if-statement,Iphone,Objective C,If Statement,我刚刚看了一个关于如何设置默认值的教程,想知道如何将默认值输出到文本。我的问题是:我可以在if语句中使用默认值吗。我试过这个: -(IBAction)press { cruzia.hidden = 0; textarea.hidden = 0; if ([defaults stringForKey:kMusic]) == YES { CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef

我刚刚看了一个关于如何设置默认值的教程,想知道如何将默认值输出到文本。我的问题是:我可以在if语句中使用默认值吗。我试过这个:

-(IBAction)press {
cruzia.hidden = 0;
textarea.hidden = 0;
if ([defaults stringForKey:kMusic]) == YES {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"click", CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

但它不起作用。它说“使用未定义的标识符‘defaults’”和‘expected expression’。我尝试将代码移到‘defaults’声明的下方,但没有任何区别。我希望有人能回复

将默认值替换为
[NSUserDefaults standardUserDefaults]
。但如果您要求返回字符串,则无法将其与布尔值进行比较。但是您可以使用
setBool:forKey:
boolForKey:

在userDefaults中存储布尔值,上面的代码有很多问题。首先,让我指出if语句和函数都没有右括号。然后,
==YES
在括号外。接下来,您将尝试将
NSString
的实例与布尔值进行比较。最后,既没有声明
默认值
也没有声明
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);
    }
}
现在,在您复制并替换旧代码之前,您应该了解我所做的假设。我假设,
默认值
以前没有被声明和实例化。最后,我假设您正在寻找一个用字符串键“kMusic”存储的布尔值,因此在代码的其他地方您可以使用类似
[[NSUserDefaults standardUserDefaults]setBool:true-forKey:@“kMusic”]如果这不是你想要的,你需要做出相应的改变


最后,下一次,在将代码带到堆栈溢出之前,请重新阅读您的代码是否存在拼写错误。

显示默认值的声明和初始化。该代码看起来不会编译。请在尝试使用Objective-C编写更多内容之前先学习如何编程。从C或Java开始。谢谢@Khansef!我正在尝试,但我不知道如何在代码中使用它。这是我失败的尝试:如果([[NSUserDefaults standardUserDefaults]stringForKey:kMusic])==YES{CFBundleRef mainBundle=CFBundleGetMainBundle();CFURLRef soundFileURLRef;soundFileURLRef=CfBundleCopyresourcell(mainBundle,(CFStringRef)@“点击”,CFSTR(“wav”),NULL);UInt32 soundID;AudioServicesCreateSystemSoundID(soundFileURLRef,&soundID);AudioServicesPlaySystemSound(soundID);如果您想添加代码,请编辑问题,没有人愿意阅读评论中的混乱内容。以下是指向apple文档的链接,您应该在继续之前仔细阅读:“首选项和设置编程指南”