【问题标题】:Attempt to insert non-property list object尝试插入非属性列表对象
【发布时间】:2021-03-22 05:17:09
【问题描述】:

当我尝试运行模拟器时,Swift 出现此错误:

"Attempt to insert non-property list object (\n    
\"grrrr.Flashcard(question: \\\"What continent is Spain in?\\\", answer: 
 \\\"Europe.\\\")\"\n) for key flashcards"

代码如下:

import UIKit

struct Flashcard {
    var question: String
    var answer: String
}

class ViewController: UIViewController {
    

@IBOutlet weak var frontLabel: UILabel!
@IBOutlet weak var backLabel: UILabel!
@IBOutlet weak var prevButton: UIButton!
@IBOutlet weak var nextButton: UIButton!


var flashcards = [Flashcard]()

var currentIndex = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    readSavedFlashcards()
    
    if flashcards.count == 0 {
    updateFlashcard(question: "What continent is Spain in?", answer: "Europe.")
    } else {
        updateLabels()
        updateNextPrevButtons()
    }
}
@IBAction func didTapOnFlashcard(_ sender: Any) {
    if frontLabel.isHidden {
        frontLabel.isHidden = false
    }
    else {
        frontLabel.isHidden = true
    }
}

func updateFlashcard(question: String, answer: String) {
    let flashcard = Flashcard(question: question, answer: answer)
    frontLabel.text = flashcard.question
    backLabel.text = flashcard.answer
    flashcards.append(flashcard)
    
    print("???? Added new flashcard")
    print("???? We now have \(flashcards.count) flashcards")
    
    currentIndex = flashcards.count - 1
    print("???? Our current index is \(currentIndex)")
    
    updateNextPrevButtons()
    
    updateLabels()
    
    saveAllFlashcardsToDisk()
}
    func updateNextPrevButtons() {
        if currentIndex == 0 {
            prevButton.isEnabled = false
            print("If statement works")
        } else {
            prevButton.isEnabled = true 
        }
        if currentIndex == flashcards.count - 1 {
            nextButton.isEnabled = false
        } else {
            nextButton.isEnabled = true
        }
    }
func updateLabels() {
    let currentFlashcard = flashcards[currentIndex]
    
    frontLabel.text = currentFlashcard.question
    backLabel.text = currentFlashcard.answer
}

@IBAction func didTapOnPrev(_ sender: Any) {
    currentIndex = currentIndex - 1
    updateLabels()
    updateNextPrevButtons() 
}

@IBAction func didTapOnNext(_ sender: Any) {
    currentIndex = currentIndex + 1
    updateLabels()
    updateNextPrevButtons()
}

func saveAllFlashcardsToDisk() {
    
    let dictionaryArray = flashcards.map { (card) -> [String: String] in return ["question": card.question, "answer": card.answer]
    }
    UserDefaults.standard.set(flashcards, forKey: "flashcards")
    print("???? Flashcards saved to UserDefaults")
}

func readSavedFlashcards() {
    
    if let dictionaryArray = UserDefaults.standard.array(forKey: "flashcards") as? [[String: String]] {
        let savedCards = dictionaryArray.map { dictionary -> Flashcard in return Flashcard(question: dictionary["question"]!, answer: dictionary["answer"]!)
        }
        flashcards.append(contentsOf: savedCards)
    }
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    
    let navigationController = segue.destination as! UINavigationController
    
    let creationController = navigationController.topViewController as! CreationViewController
    
    creationController.flashcardsController = self
    }
}

【问题讨论】:

    标签: swift


    【解决方案1】:

    这实际上是一个错字。您要保存映射的[[String:String]] 数组,因此将flashcards 替换为dictionaryArray

    func saveAllFlashcardsToDisk() {
        
        let dictionaryArray = flashcards.map { (card) -> [String: String] in return ["question": card.question, "answer": card.answer]
        }
        UserDefaults.standard.set(dictionaryArray, forKey: "flashcards")
        print("? Flashcards saved to UserDefaults")
    }
    

    考虑使用Codable 序列化自定义结构。

    【讨论】:

      猜你喜欢
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多