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

时间:2015-05-18 17:41:42

标签: swift nsuserdefaults

当我保存到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
    }


}

4 个答案:

答案 0 :(得分:2)

您的问题是您正在设置字符串,然后尝试检索字典,但我建议使用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")
}

答案 1 :(得分:1)

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

您正在将字符串"answersSaved"写入NSUserDefaults。我认为你的意思是answersSaved

答案 2 :(得分:1)

  

您应该将其选择性地转换为[String:String]字典。

试试这个:

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

答案 3 :(得分:-1)

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

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

这将它保存为通用的NSObject。如果要使用以下命令读取数据:

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

这应指定对象是字典