Swift Nscoding Not Working

Swift NSCoding Not working

Quite a bit has changed with Swift since this question was asked. I can't reproduce the error you're seeing, but I was able to get NSCoding to work with Swift 1.0 with the code below.

class CourseList: NSObject, NSCoding
{
var myCourses: Dictionary<String, String>?

override init() {}

required init(coder aDecoder: NSCoder) {
self.myCourses = aDecoder.decodeObjectForKey("myCourses") as? Dictionary
}

func encodeWithCoder(aCoder: NSCoder) {
if let courses = self.myCourses{
aCoder.encodeObject(courses, forKey: "myCourses")
}
}

func populateCourses() {
self.myCourses = ["cs101": "Hello World"]
}

func save() {
let data = NSKeyedArchiver.archivedDataWithRootObject(self)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "courseList")
}

func clear() {
NSUserDefaults.standardUserDefaults().removeObjectForKey("courseList")
}

class func loadSaved() -> CourseList? {
if let data = NSUserDefaults.standardUserDefaults().objectForKey("courseList") as? NSData {
return NSKeyedUnarchiver.unarchiveObjectWithData(data) as? CourseList
}
return nil
}
}

To load a saved instance or create a new one if needed

    // Try loading a saved version first
if let courseLs = CourseList.loadSaved() {
println("loaded Save CourseList")
} else {
// Create a new Course List
let courseLs: CourseList = CourseList()
courseLs.populateCourses()
courseLs.save()
}

Swift - NSCoding archive does not work

You need your entire object graph to conform to NSCoding. That means that all the sub-objects that you try to encode, like your Day object, also need to implement init(coder:) and encode(coder:).

In order to conform to NSCoding, your objects need to be NSObjects. I'm guessing that your Day object does not conform to NSCoding.

Does not conform to protocol 'NSCoding' - Swift 3

The encode method in Swift 3 has been renamed to

func encode(with aCoder: NSCoder) 

When you get the do not conform error you can easily find out which required methods are missing

  • Press ⌘B to build the code.
  • Press ⌘4 to show the issue navigator.
  • Click on the disclosure triangle in front of the issue line.

Swift NSCoding issue with single custom object

The issue was with the guard statments stopping the initialization of the object being unarchived. I changed from guard statements to let statements assigning an empty string if it couldn't decode the value as Michael suggested in the comments above. This helped me find the value that had an improper key so I could update the code. All is well now.

NSCoding in Swift not archiving data properly?

I Usually decode Int like this

if decoder.containsValue(forKey: "teamNumber"){
self.teamNumber = decoder.decodeInteger(forKey: "teamNumber")
}

translated to your issue will be like this, change the init?(coder aDecoder: NSCoder)method of your Assignment class

required init?(coder aDecoder: NSCoder) {

self.name = ""
self.grade = 0
self.weight = 0
super.init()

if let archivedName = aDecoder.decodeObject(forKey: "assignmentName") as? String{
self.name = archivedName
}

if decoder.containsValue(forKey: "assignmentGrade"){
self.grade = decoder.decodeInteger(forKey: "assignmentGrade")
}

if decoder.containsValue(forKey: "assignmentWeight"){
self.weight = decoder.decodeInteger(forKey: "assignmentWeight")
}

}

Hope this helps

NSCoding is not supported when trying to use KWStepper custom UI stepper pod

That code shows that this custom stepper was never intended to be instantiated from the storyboard. Don't instantiate the stepper from the storyboard! Instantiate it only in code, as intended. Follow the example code on the github site:

stepper = KWStepper(decrementButton: decrementButton, incrementButton: incrementButton)


Related Topics



Leave a reply



Submit