[英]NSUserDefaults Custom object - Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')

I must be incorrectly using Custom Objects for NSUserDefaults. The error " Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')". Below is my code, the Goal class is of particular interest, since this is where I am adopting the NSCoding protocol.


This code is global.


func saveGoals (goals : [Goal]) {
    var updatedGoals = NSKeyedArchiver.archivedDataWithRootObject(goals)
    NSUserDefaults.standardUserDefaults().setObject(updatedGoals, forKey: "Goals")

func loadCustomObjectWithKey() -> [Goal] {
    if let encodedObject : NSData = NSUserDefaults.standardUserDefaults().objectForKey("Goals") as? NSData {
        var encodedObject : NSData? = NSUserDefaults.standardUserDefaults().objectForKey("Goals") as? NSData
        var goal : [Goal] = NSKeyedUnarchiver.unarchiveObjectWithData(encodedObject!) as [Goal]
        return goal
    } else {
        return [Goal]()

This code is in GoalsViewController.


class GoalsViewController: MainPageContentViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tableView: GoalsTableView!

    var cell = GoalTableViewCell()

    var goalsArray : Array<Goal> = [] //

    override func viewDidLoad() {
        self.tableView.delegate = self
        self.tableView.dataSource = self

        if var storedGoals: [Goal] = loadCustomObjectWithKey() as [Goal]? {
            goalsArray = storedGoals
        //retrieve data.

        var goal = Goal(title: "Walk the Dog")


        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableViewAutomaticDimension

        var notification = NSNotificationCenter.defaultCenter()

        notification.addObserver(self, selector: "finishCreatingGoal:", name: "FinishCreatingGoal", object: nil)

func finishCreatingGoal(notification : NSNotification) {
        if (notification.name == "FinishCreatingGoal") {
            var userInfo = notification.userInfo!
            var text = userInfo["text"]! as String
            var index = userInfo["index"]! as Int

            var cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as GoalTableViewCell
            goalsArray[index].title = cell.goalTextField.text

            self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0), atScrollPosition: UITableViewScrollPosition.Middle, animated: true)

This code is in the Goal class.


import UIKit

class Goal : NSObject, NSCoding {

    var title : String? = ""
    var checkmarked : Bool? = false 
    var isLastCell : Bool? = false 
    var enabled : Bool? = true 

    var priority = Priority.defaultPriority

    override init() {

    init(title : String) {
        self.title = title

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(title!, forKey: "title")
        aCoder.encodeBool(checkmarked!, forKey: "checkmarked")
        aCoder.encodeBool(isLastCell!, forKey: "isLastCell")
        aCoder.encodeBool(enabled!, forKey: "enabled")

    required init(coder aDecoder: NSCoder) {
        title = aDecoder.decodeObjectForKey("title") as String!
        checkmarked = aDecoder.decodeBoolForKey("checkmarked") as Bool
        isLastCell = aDecoder.decodeBoolForKey("isLastCell") as Bool
        enabled = aDecoder.decodeBoolForKey("enabled") as Bool


1 个解决方案



I am going to just copy code from a working project I have:


here is the Game object class with data from a math flash card game:


import Foundation

class GameData: NSObject {

    var sign: String = "+"
    var level: Int = 1
    var problems: Int = 10
    var time: Int = 30
    var skipWrong: Bool = true
    var usedTime: Int = 0
    var correctCount: Int = 0
    var correctTopNumber: [Int] = [Int]()
    var correctBottomNumber: [Int] = [Int]()
    var wrongTopNumber: [Int] = [Int]()
    var wrongBottomNumber: [Int] = [Int]()
    var date: NSDate = NSDate()

    func encodeWithCoder(aCoder: NSCoder!) {
        aCoder.encodeObject(sign, forKey: "sign")
        aCoder.encodeInteger(level, forKey: "level")
        aCoder.encodeInteger(problems, forKey: "problems")
        aCoder.encodeInteger(time, forKey: "time")
        aCoder.encodeBool(skipWrong, forKey: "skipWrong")
        aCoder.encodeInteger(usedTime, forKey: "usedTime")
        aCoder.encodeInteger(correctCount, forKey: "correctCount")
        aCoder.encodeObject(correctTopNumber, forKey: "correctTopNumber")
        aCoder.encodeObject(correctBottomNumber, forKey: "correctBottomNumber")
        aCoder.encodeObject(wrongTopNumber, forKey: "wrongTopNumber")
        aCoder.encodeObject(wrongBottomNumber, forKey: "wrongBottomNumber")
        aCoder.encodeObject(date, forKey: "date")

    init(coder aDecoder: NSCoder!) {
        sign = aDecoder.decodeObjectForKey("sign") as String
        level = aDecoder.decodeIntegerForKey("level")
        problems = aDecoder.decodeIntegerForKey("problems")
        time = aDecoder.decodeIntegerForKey("time")
        skipWrong = aDecoder.decodeBoolForKey("skipWrong")
        usedTime = aDecoder.decodeIntegerForKey("usedTime")
        correctCount = aDecoder.decodeIntegerForKey("correctCount")
        correctTopNumber = aDecoder.decodeObjectForKey("correctTopNumber") as Array
        correctBottomNumber = aDecoder.decodeObjectForKey("correctBottomNumber") as Array
        wrongTopNumber = aDecoder.decodeObjectForKey("wrongTopNumber") as Array
        wrongBottomNumber = aDecoder.decodeObjectForKey("wrongBottomNumber") as Array
        date = aDecoder.decodeObjectForKey("date") as NSDate

    override init() {

This part looks about the same as yours, but with more variable types. The archiver and retriever classes differ from you:


import Foundation

class ArchiveGameData:NSObject {

    var documentDirectories:NSArray = []
    var documentDirectory:String = ""
    var path:String = ""

    func ArchiveResults(#dataSet: [GameData]) {
        documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        documentDirectory = documentDirectories.objectAtIndex(0) as String
        path = documentDirectory.stringByAppendingPathComponent("results3.archive")

        if NSKeyedArchiver.archiveRootObject(dataSet, toFile: path) {
            //println("Success writing to file!")
        } else {
            println("Unable to write to file!")

    func RetrieveGameData() -> NSObject {
        var dataToRetrieve = [GameData]()
        documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        documentDirectory = documentDirectories.objectAtIndex(0) as String
        path = documentDirectory.stringByAppendingPathComponent("results3.archive")
        if let dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? [GameData] {
          dataToRetrieve = dataToRetrieve2

Finally, the code for storing and retrieving from within a ViewController:


    var gameDataArray = ArchiveGameData().RetrieveGameData() as [GameData]

      gameData = GameData() //create local object then append all the new data, then store it
gameData.sign = buttonStates.sign
        gameData.level = buttonStates.level
        gameData.problems = buttonStates.problems
        gameData.time = buttonStates.time
//etc. for all properties

      ArchiveGameData().ArchiveResults(dataSet: gameDataArray)



Type is an interface or abstract class and cannot be instantiated. c# send a object with contain a list of abstract object Attempt to set a non-property-list object - NSUserDefaults How to archive and unarchive custom objects in Swift? Or how to save custom object to NSUserDefaults in Swift? Transform an array of objects in an object list with a property of the object as key how can store custom objects in NSUserDefaults How do I iterate through an object which contain children objects with the same type as the object? Delphi 2009 - Custom Enum type property in the Object Inspector Cannot convert type 'object[][]' to 'System.Collections.Generic.List' App crashes when storing custom object in NSUserDefaults Sort ArrayList of custom Objects by property
粤ICP备14056181号  © 2014-2021 ITdaan.com