如何知道是否存在 NSUserDefault?
我用什么条件才能知道我使用的 NSUserDefaults ?
NSUserDefaults *preftest;
preftest=[NSUserDefaults standardUserDefaults];
NSString *opening = [preftest stringForKey:@"opening"];
这就是我获取数据的方式。如果前缀 stringForKey:@"opening" 尚不存在,我想进入 if 条件......所以我尝试了:
if (opening == nil) {
...
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
[pref setObject:@"OK" forKey:@"pass"];
NSString *finalfr = [pref stringForKey:@"opening"];
NSLog(@"(here's the new value of opening: %@ )", finalfr);
...
}
if (opening != nil) {
NSLog(@" != nil");
// http request}
但事实上,它不起作用。
谢谢你帮我!!
What condition do I use to know it the NSUserDefaults I use?
NSUserDefaults *preftest;
preftest=[NSUserDefaults standardUserDefaults];
NSString *opening = [preftest stringForKey:@"opening"];
This is the way I'm getting the datas. I want to get into the if condition if the preftext stringForKey:@"opening" doesn't already exist.... So I tried :
if (opening == nil) {
...
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
[pref setObject:@"OK" forKey:@"pass"];
NSString *finalfr = [pref stringForKey:@"opening"];
NSLog(@"(here's the new value of opening: %@ )", finalfr);
...
}
if (opening != nil) {
NSLog(@" != nil");
// http request}
But inded, it doesn't work.
Thanks to help me !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但是您正在使用
opening != nil
测试它是否存在。如果你想测试它是否不存在,则可以是
opening == nil
或只是!opening
。But you are testing if it does exist with
opening != nil
.If you want to test if it does NOT exist, it would be
opening == nil
or just!opening
.也许您可以不将指针与 nil 进行比较,而是将内容与空字符串进行比较 [opening isEqualToString: @""],它返回一个布尔值。
Maybe instead of comparing the pointer against nil, you could compare the contents against an empty string [opening isEqualToString: @""], which returns a boolean.