2017-04-25 27 views
-5

可能有人請解釋一下怎麼用UserDefaults在Swift3來完成這個任務:的iOS斯威夫特3 - 如何正確使用UserDefalts

我想有一個變量幣= 0與顯示「硬幣標籤:0 「,然後有一個按鈕,並點擊更新硬幣1。

我需要這個被保存爲一個UserDefault,以便您可以關閉視圖/應用程序,並有金額保持加載/能夠添加更多給他們。

感謝先進!

+0

這個問題有很多部分。你到底需要什麼幫助?有無數使用'UserDefaults'的例子。你至少看過參考文檔嗎? – rmaddy

+0

https://iosdevcenters.blogspot.com/2016/05/save-data-using-nsuserdefaults-in-swift。html –

回答

0
var coins = 0 
    let saveData = UserDefaults.standard 
    saveData.set(coins, forKey: "string you want to use to refer to this save data") 
+0

你忘了'synchronize()'調用。 –

+0

我以前沒有用過這個電話,也沒有遇到過用戶默認的問題,但是我不知道該怎麼看 –

+0

https://developer.apple.com/reference/foundation/userdefaults/1414005-synchronize 它在週期性級別被調用操作系統,但仍然習慣於在手動完成後立即打電話。 –

1

使用UserDefaults時的一個好主意是將其包裝在協議中並使用枚舉作爲鍵。以下是您的coinsCount以及其他一些示例的枚舉案例。

enum Key: String { 
    case coinsCount 
    case userId 
    case recentSearches 

    static var all: [Key] = [.coinsCount, .userId, .recentSearches] 
} 

protocol PersistenceManagerProtocol { 

    //MARK: - Delete 
    func deleteAll() 
    func deleteValue(for key: Key) 

    //MARK: - Inspection 
    func hasValue(for key: Key) -> Bool 

    //MARK: - Save 
    func save(_ value: Any, for key: Key) 

    //MARK: - Load 
    func load<Value>(with key: Key) -> Value? 
    func loadArray<Value>(with key: Key) -> [Value] 
} 


final class PersistenceManager { 
    fileprivate let userDefaults: UserDefaults 

    init(userDefaults: UserDefaults) { 
     self.userDefaults = userDefaults 
    } 
} 

extension PersistenceManager: PersistenceManagerProtocol { 

    //MARK: - Delete 
    func deleteValue(for key: Key) { 
     userDefaults.setValue(nil, forKey: key.rawValue) 
    } 

    func deleteAll() { 
     for key in Key.all { 
      deleteValue(for: key) 
     } 
    } 

    //MARK: - Inspection 
    func hasValue(for key: Key) -> Bool { 
     return userDefaults.value(forKey: key.rawValue) != nil 
    } 

    //MARK: - Save 
    func save(_ value: Any, for key: Key) { 
     saveValue(value, for: key) 
    } 

    //MARK: - Load 
    func load<Value>(with key: Key) -> Value? { 
     guard let value = userDefaults.value(forKey: key.rawValue) as? Value else { return nil } 
     return value 
    } 

    func loadArray<Value>(with key: Key) -> [Value] { 
     guard let array: [Value] = load(with: key) else { return [] } 
     return array 
    } 
} 

//USAGE 
let persistenceManager: PersistenceManagerProtocol = PersistenceManager(.standard) // using dependency injection, can mock data 

persistenceManager.save(237, for: .coinsCount) 
let coinsCount: Int = persistenceManager.load(with: .coinsCount) 

persistenceManager.save("foobar", for: .userId) 
let userId: String = persistenceManager.load(with: .userId) 

persistenceManager.save(["foo", "bar"], .recentSearches) 
let recentSearches: [String] = persistenceManager.loadArray(with: .recentSearches) 
+0

@ jacob-peterson它怎麼樣了? – Sajjon

-1

嘗試這樣:

let defaults = UserDefaults() //Declares user defaults 

let savedCoins = defaults.integer(forKey: "userCoins") //declares a variable for your key 
savedCoins += 1 //add one when your button is clicked 
defaults.set(savedCoins, forKey: "userCoins") //re-saves the new value 

每當你想找回您的默認值中值:當你想要的值設置爲默認值

let savedCoins = defaults.integer(forKey: "userCoins") 

defaults.set(savedCoins, forKey: "userCoins") 

確保你的鑰匙是一樣的!

+0

我覺得當你設置你需要使用'savedCoins'而不是'coins'的值時 –

-2

我也遇到了麻煩,使用NSUserDefaults的,我在斯威夫特不是專家,但我可以告訴你,我的理解它,它是如何工作對我來說:

UserDefaults分兩步工作:

第一步:設置要保存數據,並在: 例如,如果你要保存的東西,而按下一個按鈕:

buttonPressed {

我們erdefaults.standard.set(列表,forKey:「anyKeyYouWantHere」) - >然而,「列表」變量看起來
像此刻你按下按鈕會就這樣

}

被保存第二步

如果例如我想加載視圖加載時,在ViewDidLoad我只是給變量「列表」返回它剛好在我關閉應用程序之前的值;

像這樣:

列表= UserDefaults.standard.string(forKey: 「theSameKeyFromStep1」)

希望這是有道理的你!祝你好運!