簡體   English   中英

如何保存iPhone應用程序的用戶首選項?

[英]How do I save user preferences for my iPhone app?

問題標題幾乎讓它消失了 - 我希望我的應用程序記住一些事情。 它是某種計算器,因此它應該保存最后使用的值和一些用戶可選擇的設置。

基本上我想保存一些浮動和BOOL並在下次加載應用程序時再次加載它們。

什么是最好最簡單的方法?

謝謝!!

最簡單的方法之一是將其保存在NSUserDefaults

設置:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:value 
                 forKey:key];
// – setBool:forKey:
// – setFloat:forKey:  
// in your case 
[userDefaults synchronize];

獲得:

[[NSUserDefaults standardUserDefaults] objectForKey:key];

– boolForKey:

– floatForKey:在你的情況下。

除了非常好的NSUserDefaults方法之外,還有另一種簡單的方法可以將NSArray,NSDictionary或NSData中的數據存儲在一個文件中。 您也可以使用這些方法:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

分別(對於NSDictionary):

+ (id)dictionaryWithContentsOfFile:(NSString *)path

你只需要提供一個有效的路徑到一個位置。 根據iOS應用程序編程指南,/ Library / Caches目錄是存儲應用程序啟動之間需要保留的數據的最佳位置。 (見這里

為了在您的文檔directoy中存儲/加載名為“managers”的字段中的字典,您可以使用以下方法:

-(void) loadDictionary {
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cacheDirectory = [paths objectAtIndex:0];
    //create a destination file name to write the data :
    NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory];
    NSDictionary* panelLibraryContent = [NSDictionary dictionaryWithContentsOfFile:fullFileName];
    if (panelLibraryContent != nil) {
        // load was successful do something with the data...
    } else {
        // error while loading the file
    }   
}

-(void) storeDictionary:(NSDictionary*) dictionaryToStore {
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cacheDirectory = [paths objectAtIndex:0];
    //make a file name to write the data to using the
    //cache directory:
    NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory];

    if (dictionaryToStore != nil) {
        [dictionaryToStore writeToFile:fullFileName atomically:YES];
    }
} 

無論如何,這種方法非常有限,如果你想存儲更復雜的數據,你必須花費大量的額外工作。 在這種情況下,CoreData API非常方便。

在Swift中:

設置

let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(value, forKey: key)

// userDefaults.setFloat(12.34, forKey: "myFloatKey")
// userDefaults.setBool(true, forKey: "myBoolKey")

請注意,對於iOS 8及更高版本, 建議不要調用userDefaults.synchronize()

入門

let userDefaults = NSUserDefaults.standardUserDefaults()
if let value = userDefaults.objectForKey(key) {
    print(value)
}

請注意, userDefaults.boolForKeyuserDefaults.floatForKey都返回非可選值,因此它們永遠不會為nil (僅為false0.0 )。

進一步閱讀

您正在尋找NSUserDefaults

Swift 4 / Linux

顯然有些事情發生了變化。 現在有UserDefault類。 檢查以下鏈接:

https://developer.apple.com/documentation/foundation/userdefaults

https://www.hackingwithswift.com/read/12/2/reading-and-writing-basics-userdefaults

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM