当我在 Swift 中保存到 NSUserDefaults 时,NSDictionary 变为 NSCFString

NSDictionary becomes NSCFString when I save to NSUserDefaults in Swift

当我保存到 NSUserDefaults 时,NSDictionary 变成 NSCFString:

class SecondViewController: UIViewController {

    @IBOutlet var questionforsave: UITextField!
    @IBOutlet var answerforsave: UITextField!

    var answersSaved = [String:String]()

    @IBAction func savePressed(sender: AnyObject) {

        if questionforsave.text.isEmpty || answerforsave.text.isEmpty{

            let alert = UIAlertView()
            alert.title = "Empty fields"
            alert.message = "Please Enter Text In Both Text Fields"
            alert.addButtonWithTitle("Ok")
            alert.show()
        }else{

            println("is ok")
            var answerSave = answerforsave.text
            var questionSave = questionforsave.text


            answersSaved[questionSave] = answerSave

            let alert = UIAlertView()
            alert.title = "Success!"
            alert.message = "It worked!"
            alert.addButtonWithTitle("Awesome!")
            alert.show()

            NSUserDefaults.standardUserDefaults().setObject(answersSaved, forKey: "answersSaved")
            println(answersSaved)


        }


    }


    override func viewDidLoad() {
        super.viewDidLoad()

        println(answersSaved)



        if NSUserDefaults.standardUserDefaults().objectForKey("answersSaved") != nil {
            answersSaved = NSUserDefaults.standardUserDefaults().objectForKey("answersSaved") as! [String:String]

        }else{
            println("No Defaults")
        }




    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
    }
    func textFieldShouldReturn(textField: UITextField!) -> Bool {
        //do the stuff here
        resignFirstResponder()
        return true
    }


}
 NSUserDefaults.standardUserDefaults().setObject("answersSaved", forKey: "answersSaved")

您正在将字符串 "answersSaved" 写入 NSUserDefaults。我假设你的意思是 answersSaved.

当你将字典加载到 NSUserDefaults 时,你应该这样做:

NSUserDefaults.standardUserDefaults().setObject(answersSaved, forKey: "answersSaved")

这会将其保存为通用 NSObject。当你想读出数据时使用命令:

answersSaved = NSUserDefaults.standardUserDefaults().dictionaryForKey("answersSaved")

这应该指定该对象是一个字典

您的问题是您正在设置一个字符串,然后尝试检索字典,但我建议使用 if let 检索字典,然后可选择将其转换为 [String: String]:

var answersSaved = [String: String]()

override func viewDidLoad() {
    super.viewDidLoad()

    if let answersFromDefaults = NSUserDefaults.standardUserDefaults().dictionaryForKey("answersSaved") as? [String: String] {
        answersSaved = answersFromDefaults
    } else {
        // Failed to retrieve value from NSUserDefaults
    }

    answersSaved = ["test": "a"] // Set answersSaved somewhere in your code
}

@IBAction func saveButtonPressed(sender: UIBarButtonItem) {
    NSUserDefaults.standardUserDefaults().setObject(answersSaved, forKey: "answersSaved")
}

You should cast it optionally as a [String:String] dictionary.

试试这个:

if let answersFromDefaults = NSUserDefaults.standardUserDefaults().dictionaryForKey("answersSaved") as?      [String: String] {
    answersSaved = answersFromDefaults
} else {
    // Failed to retrieve value from NSUserDefaults
}