iosarraysswiftnsobjectnscoder

Encode Array with NSCoder


I have a User Class that uses a saveuser() method whenever the application terminates. The User has two Arrays of Custom Classes that sub-class NSObject. Here is my encode method.

func encode(with aCoder: NSCoder) {
    aCoder.encode(self.firstName, forKey: coderKey.fName)
    aCoder.encode(self.lastName, forKey: coderKey.lName)
    aCoder.encode(self.bio, forKey: coderKey.bio)
    aCoder.encode(self.tags, forKey: coderKey.tags)
    aCoder.encode(self.organizations, forKey: coderKey.orgs)
    aCoder.encode(self.img, forKey: coderKey.img)
}

The app Crashes when encoding self.tags. I assume it will do the same with self.organizations because it is also an array of NSObjects and possibly with self.img because it is a UIImage. Here is the error I am getting.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Social_Justice.Tag encodeWithCoder:]: unrecognized selector sent to instance 0x60000005efc0'

What should I do to resolve this issue? If you need to see any more code, just comment and i'll edit.


Solution

  • as David Berry commented on the original post. You have to Make sure any custom classes that you are trying to encode, also conform to NSCoder. They don't need to have archive paths, they just need to have the encode and decode functions.