NSDictionary* defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; NSLog(@"Defaults: %@", defaults);
是用来获取设备上的所有的NSUserDefaults的设置。
上面代码输出了
2013-06-26 12:41:09.948 MyText[5180:c07] Defaults: { AppleITunesStoreItemKinds = ( wemix, podcast, document, "software-update", newsstand, "tv-episode", "tv-season", software, audiobook, movie, song, album, tone, booklet, ringtone, artist, "podcast-episode", "itunes-u", mix, eBook ); AppleLanguages = ( en, "zh-Hans", "zh-Hant", fr, de, ja, nl, it, es, pt, "pt-PT", da, fi, nb, sv, ko, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, "en-GB", ca, hu, vi ); AppleLocale = "en_US"; NSInterfaceStyle = macintosh; NSLanguages = ( en, "zh-Hans", "zh-Hant", fr, de, ja, nl, it, es, pt, "pt-PT", da, fi, nb, sv, ko, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, "en-GB", ca, hu, vi ); }
如果想单独看某个key的设置,例如:
NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleKeyboards"]; NSLog(@"Keyboards: %@", array);
会输出
AppleKeyboards = ( "zh_Hans-Pinyin@sw=Pinyin;hw=US", "en_US@hw=US;sw=QWERTY" );
用法:
//保存数据到NSUserDefaults -(void)saveNSUserDefaults { NSString *myString = @"enuola"; int myInteger = 100; float myFloat = 50.0f; double myDouble = 20.0; NSDate *myDate = [NSDate date]; NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world", nil]; NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"enuo", @"20", nil] forKeys:[NSArray arrayWithObjects:@"name", @"age", nil]]; //将上述数据全部存储到NSUserDefaults中 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; //存储时,除NSNumber类型使用对应的类型意外,其他的都是使用setObject:forKey: [userDefaults setInteger:myInteger forKey:@"myInteger"]; [userDefaults setFloat:myFloat forKey:@"myFloat"]; [userDefaults setDouble:myDouble forKey:@"myDouble"]; [userDefaults setObject:myString forKey:@"myString"]; [userDefaults setObject:myDate forKey:@"myDate"]; [userDefaults setObject:myArray forKey:@"myArray"]; [userDefaults setObject:myDictionary forKey:@"myDictionary"]; //这里建议同步存储到磁盘中,但是不是必须的 [userDefaults synchronize]; } //从NSUserDefaults中读取数据 -(void)readNSUserDefaults { NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults]; //读取数据到各个label中 //读取整型int类型的数据 NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"]; txtInteger.text = [NSString stringWithFormat:@"%d",myInteger]; //读取浮点型float类型的数据 float myFloat = [userDefaultes floatForKey:@"myFloat"]; txtFloat.text = [NSString stringWithFormat:@"%f",myFloat]; //读取double类型的数据 double myDouble = [userDefaultes doubleForKey:@"myDouble"]; txtDouble.text = [NSString stringWithFormat:@"%f",myDouble]; //读取NSString类型的数据 NSString *myString = [userDefaultes stringForKey:@"myString"]; txtNSString.text = myString; //读取NSDate日期类型的数据 NSDate *myDate = [userDefaultes valueForKey:@"myDate"]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; txtNSDate.text = [NSString stringWithFormat:@"%@",[df stringFromDate:myDate]]; //读取数组NSArray类型的数据 NSArray *myArray = [userDefaultes arrayForKey:@"myArray"]; NSString *myArrayString = [[NSString alloc] init]; for(NSString *str in myArray) { NSLog(@"str= %@",str); myArrayString = [NSString stringWithFormat:@"%@ %@", myArrayString, str]; [myArrayString stringByAppendingString:str]; // [myArrayString stringByAppendingFormat:@"%@",str]; NSLog(@"myArrayString=%@",myArrayString); } txtNSArray.text = myArrayString; //读取字典类型NSDictionary类型的数据 NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"]; NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%d",[myDictionary valueForKey:@"name"], [[myDictionary valueForKey:@"age"] integerValue]]; txtNSDictionary.text = myDicString; }
用NSUserDefaults存储的数据下次程序运行的时候依然存在,它把数据存储在什么地方了?如何能够清除?
其实它存储在应用程序内置的一个plist文件里,这个可以根据路径看到。
比如说这个是你的程序沙盒位置
/UsersLibrary/Application Support/iPhoneSimulator/4.1/Applicati*****/29788E40-AF47-45A0-8E92-3AC0F501B7F4/,(这个是应用程序对应在mac上的位置)
这个下面有/Library/Prefereces,里面有个plist文件,存储的就是你的userDefaults
想要删掉的话,用removeObjectForKey或者删掉沙盒,也就是你的应用程序然后重新安装。
这个plist文件在NsUserDefaults使用之前是没有的。
CCUserDefault和NSUserDefaults差不多,地址是:
std::string dir = CCUserDefault::sharedUserDefault()->getXMLFilePath(); CCLOG("dir:%s",dir.c_str());
dir:/Users/liulijun/Library/Application Support/iPhone Simulator/6.1/Applications/5135A000-024F-468E-99F0-F75FABC1392A/Library/Caches/UserDefault.xml
方法:
/** @brief Set bool value by key. */ void setBoolForKey(const char* pKey, bool value); /** @brief Set integer value by key. */ void setIntegerForKey(const char* pKey, int value); /** @brief Set float value by key. */ void setFloatForKey(const char* pKey, float value); /** @brief Set double value by key. */ void setDoubleForKey(const char* pKey, double value); /** @brief Set string value by key. */ void setStringForKey(const char* pKey, const std::string & value); /** @brief Save content to xml file */ void flush();
ps:
NSUserDefaults读取和写入自定义对象
http://blog.csdn.net/chyroger/article/details/5785297