NSDictionary* defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; NSLog(@"Defaults: %@", defaults);
是用來獲取設備上的全部的NSUserDefaults的設置。數組
上面代碼輸出了spa
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的設置,例如:.net
NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleKeyboards"]; NSLog(@"Keyboards: %@", array);
會輸出code
AppleKeyboards = ( "zh_Hans-Pinyin@sw=Pinyin;hw=US", "en_US@hw=US;sw=QWERTY" );
用法:orm
//保存數據到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存儲的數據下次程序運行的時候依然存在,它把數據存儲在什麼地方了?如何可以清除?
xml
其實它存儲在應用程序內置的一個plist文件裏,這個能夠根據路徑看到。對象
好比說這個是你的程序沙盒位置
/UsersLibrary/Application Support/iPhoneSimulator/4.1/Applicati*****/29788E40-AF47-45A0-8E92-3AC0F501B7F4/,(這個是應用程序對應在mac上的位置)
這個下面有/Library/Prefereces,裏面有個plist文件,存儲的就是你的userDefaults
想要刪掉的話,用removeObjectForKey或者刪掉沙盒,也就是你的應用程序而後從新安裝。blog
這個plist文件在NsUserDefaults使用以前是沒有的。ci
CCUserDefault和NSUserDefaults差很少,地址是:rem
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