ios NSUserDefaults 是否包含值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9206263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:43:06  来源:igfitidea点击:

NSUserDefaults Contains Value Or Not?

ioscocoa-touchnsuserdefaults

提问by Aswathy Bose

How to know whether NSUserDefaultscontains any value?How to check whether its empty?

如何知道是否NSUserDefaults包含任何值?如何检查是否为空?

回答by Krishnabhadra

There isn't a way to check whether an object within NSUserDefaults is empty or not. However, you can check whether a value for particular key is nil or not.

没有办法检查 NSUserDefaults 中的对象是否为空。但是,您可以检查特定键的值是否为零。

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSObject * object = [prefs objectForKey:@"your_particular_key"];
if(object != nil){
   //object is there
}

回答by Parag Bafna

NSUserDefaults *data = [NSUserDefaults standardUserDefaults];  
NSString *string = [data objectForKey:@"yourKey"];
if(string==nil)
NSlog(@"nil")  

Take a look at NSUserDefault documentation

看看NSUserDefault 文档

// For saving the values
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[userDefaults setObject:@"Ttest" forKey:@"key"];
// --- For Retrieving

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [userDefaults stringForKey:@"key"];

回答by hamstergene

To check whether a specific value is set or not, no matter of its location (global or application's), check the returned value of -[NSUserDefaults objectForKey:]

要检查是否设置了特定值,无论其位置(全局或应用程序的)如何,请检查返回的值 -[NSUserDefaults objectForKey:]

id obj = [[NSUserDefaults standardUserDefaults] objectForKey:@"My-Key-Name"];
if (obj != nil) {...}

To check if the application (bundle) has any settings stored in user defaults:

要检查应用程序(捆绑包)是否在用户默认值中存储了任何设置:

NSUserDefaults* sdu = [NSUserDefaults standardUserDefaults];

NSString* bundleId = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary* mainBundleSettings = [sdu persistentDomainForName:bundleId];

NSLog(@"%@", mainBundleSettings);

If you are interested in all possible values for which -[NSUserDefaults objectForKey:]will return something, including system global settings, simply call

如果您对所有可能-[NSUserDefaults objectForKey:]返回的值感兴趣,包括系统全局设置,只需调用

NSDictionary* allPossibleSettings = [sdu dictionaryRepresentation];

NSUserDefaultsis never empty. It combines global settings, bundle's settings, temporary data and maybe something else. For example, if you call:

NSUserDefaults永远是空的。它结合了全局设置、bundle 的设置、临时数据以及其他一些东西。例如,如果您调用:

[[NSUserDefaults standardUserDefaults] objectForKey:@"NSBoldSystemFont"]

you will get the @"LucidaGrande-Bold"string value which will be taken from global settings, even when your application has never set this value.

您将获得@"LucidaGrande-Bold"将从全局设置中获取的字符串值,即使您的应用程序从未设置过该值。