I ran into an issue where I could not use a lazy variable inside init?(coder aDecoder: NSCoder)
My sample code is
class Category: NSObject, NSCoding {
var categoryID: NSInteger!
var categoryName: String!
var categoryLogoURL: String!
lazy var categoryTags = [String]()
private override init() {
}
required init?(coder aDecoder: NSCoder) {
self.categoryID = aDecoder.decodeInteger(forKey: "categoryID")
self.categoryName = aDecoder.decodeObject(forKey: "categoryName") as! String
self.categoryLogoURL = aDecoder.decodeObject(forKey: "categoryLogoURL") as! String
self.categoryTags = aDecoder.decodeObject(forKey: "categoryTags") as! [String]
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.categoryID, forKey: "categoryID")
aCoder.encode(categoryName, forKey: "categoryName")
aCoder.encode(categoryLogoURL, forKey: "categoryLogoURL")
aCoder.encode(categoryTags, forKey: "categoryTags")
}
}
I am getting an error Use of 'self' in property access 'categoryTags' before super.init initializes self
Once I remove lazy everything works fine. What I am doing wrong?
call Super Init:
required init?(coder aDecoder: NSCoder) {
super.init()
self.categoryID = aDecoder.decodeInteger(forKey: "categoryID")
self.categoryName = aDecoder.decodeObject(forKey: "categoryName") as! String
self.categoryLogoURL = aDecoder.decodeObject(forKey: "categoryLogoURL") as! String
self.categoryTags = aDecoder.decodeObject(forKey: "categoryTags") as! [String]
}
Related
I'm relatively new to iOS development in general, but I'm coding this app.
I have custom objects "Semester", "Course", and "Assignments". Semesters contain arrays of courses and courses contain arrays of assignments. I'm able to properly archive the semester and course information, but for some reason the assignment information can't be read.
Semester:
required init?(coder aDecoder: NSCoder) {
self.name = ""
super.init()
if let archivedName = aDecoder.decodeObject(forKey: "name") as? String {
name = archivedName
}
if let archivedCourses = aDecoder.decodeObject(forKey: "courses") as? [Course] {
courses = archivedCourses
}
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(courses, forKey: "courses")
}
Course:
required init?(coder aDecoder: NSCoder) {
self.name = ""
super.init()
if let archivedName = aDecoder.decodeObject(forKey: "courseName") as? String {
name = archivedName
}
if let archivedAssignments = aDecoder.decodeObject(forKey: "assignments") as? [Assignment] {
assignments = archivedAssignments
}
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "courseName")
aCoder.encode(assignments, forKey: "assignments")
Assignment:
required init?(coder aDecoder: NSCoder) {
self.name = ""
self.grade = 0
self.weight = 0
super.init()
if let archivedName = aDecoder.decodeObject(forKey: "assignmentName") as? String, let archivedGrade = aDecoder.decodeObject(forKey: "assignmentGrade") as? Int, let archivedWeight = aDecoder.decodeObject(forKey: "assignmentWeight") as? Int {
name = archivedName
grade = archivedGrade
weight = archivedWeight
}
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "assignmentName")
aCoder.encode(grade, forKey: "assignmentGrade")
aCoder.encode(weight, forKey: "assignmentWeight")
}
I'm not sure why but the assignment details can't be read when I try to decode the object.
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
When try to encode my custom object in iOS swift get this error from Xcode 8.3
unrecognized selector sent to instance 0x60800166fe80
*** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
And my code like this:
import UIKit
import Foundation
class Place: NSObject {
func setCustomObject(CustomObject obj:Any,Key key:String) {
let encodedObject : Data = NSKeyedArchiver.archivedData(withRootObject: obj)
UserDefaults.standard.set(encodedObject, forKey: key)
}
}
Here's an example how to make an object to conform to NSCoding. Basically you need to provide implementation of two methods - required convenience init?(coder decoder: NSCoder) and encode(with aCoder: NSCoder)
class Book: NSObject, NSCoding {
var title: String?
var pageCount: Int?
// Memberwise initializer
init(title: String,pageCount: Int) {
self.title = title
self.pageCount = pageCount
}
// MARK: NSCoding
// Here you will try to initialize an object from archve using keys you did set in `encode` method.
required convenience init?(coder decoder: NSCoder) {
guard let title = decoder.decodeObject(forKey: "title") as? String else { return nil }
self.init(title: title, pageCount: decoder.decodeInteger(forKey: "pageCount"))
}
// Here you need to set properties to specific keys in archive
func encode(with aCoder: NSCoder) {
aCoder.encode(self.title, forKey: "title")
aCoder.encodeCInt(Int32(self.pageCount), forKey: "pageCount")
}
}
Also I would recommend changing your setCustomObject method to this:
func setCustomObject(obj:NSCoding, key:String) {
let encodedObject : Data = NSKeyedArchiver.archivedData(withRootObject: obj)
UserDefaults.standard.set(encodedObject, forKey: key)
}
This way compiler prevent you passing NSKeyedArchiver an object that does not conform to NSCoding protocol.
If you don't want to provide all properties in the init method you can use default values:
init(title : String? = nil, pageCount: Int? = nil){
self.title = title
self.pageCount = pageCount
}
Now you can just init your object without any properties. Like that Book()
Here is the solutions, you have to implement the two methods
Encode Method
func encode(with aCoder: NSCoder)
Decoding method
required init?(coder aDecoder: NSCoder)
Complete Example code
class User: NSObject , NSCoding
{
var userID : Int = 0
var name : String = ""
var firstName : String = ""
var lastName : String = ""
var username : String = ""
var email : String = ""
override init(){
super.init();
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.userID, forKey: "id");
aCoder.encode(self.firstName, forKey: "first_name");
aCoder.encode(self.lastName, forKey: "last_name");
aCoder.encode(self.name, forKey: "name");
aCoder.encode(self.username,forKey: "username");
aCoder.encode(self.email, forKey: "email");
}
required init?(coder aDecoder: NSCoder) {
super.init()
self.userID = aDecoder.decodeInteger(forKey: "id");
self.firstName = aDecoder.decodeObject(forKey: "first_name") as! String;
self.lastName = aDecoder.decodeObject(forKey: "last_name") as! String;
self.name = aDecoder.decodeObject(forKey: "name") as! String
self.username = aDecoder.decodeObject(forKey: "username") as! String
self.email = aDecoder.decodeObject(forKey: "email") as! String;
}
init(data : [String: AnyObject]) {
super.init()
self.userID = String.numberValue(data["user_id"]).intValue;
self.firstName = String.stringValue(data["first_name"]);
self.lastName = String.stringValue(data["last_name"]);
self.email = String.stringValue(data["email"]);
self.username = String.stringValue(data["user_name"]);
}
class func loadLoggedInUser() -> User {
if let archivedObject = UserDefaults.standard.object(forKey:"CurrentUserAcc"){
if let user = NSKeyedUnarchiver.unarchiveObject(with: (archivedObject as! NSData) as Data) as? User {
return user;
}
}
return User()
}
func saveUser(){
let archivedObject : NSData = NSKeyedArchiver.archivedData(withRootObject: self) as NSData
UserDefaults.standard.set(archivedObject, forKey: "CurrentUserAcc");
UserDefaults.standard.synchronize();
}
func deleteUser(){
UserDefaults.standard.set(nil, forKey: "CurrentUserAcc")
UserDefaults.standard.synchronize();
}
}
I've been using apple's guide for saving data in swift, but when I try to compile it's telling me that is isn't conforming. Based on apple's guide and other code it should be.
// MARK: NSCoding
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(firstName, forKey: "firstName")
aCoder.encodeObject(lastName, forKey: "lastName")
aCoder.encodeObject(phoneNumber, forKey: "phoneNumber")
aCoder.encodeObject(email, forKey: "email")
aCoder.encodeObject(address, forKey: "address")
}
required convenience init?(coder aDecoder: NSCoder) {
firstName = aDecoder.decodeObjectForKey("firstName") as! String
lastName = aDecoder.decodeObjectForKey("lastName") as! String
phoneNumber = aDecoder.decodeObjectForKey("phoneNumber") as! String
email = aDecoder.decodeObjectForKey("email") as! String
address = aDecoder.decodeObjectForKey("addressq") as! String
self.init(phoneNumber: phoneNumber, firstName: firstName, lastName: lastName, email: email, address: address)
}
So I'm simply not sure why it isn't conforming when I can run Apple's program fine.
EDIT: So the problem seems to be that I didn't have my class inherited from NSObject. That solved the problem.
No errors here:
class Dog {
var firstName: String
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(firstName, forKey: "firstName")
}
init(firstName: String) {
self.firstName = firstName
}
required convenience init?(coder aDecoder: NSCoder) {
let x = aDecoder.decodeObjectForKey("firstName") as! String
self.init(firstName: x)
}
}
var d = Dog(firstName: "Rover")
I'm trying to use NSKeyedArchiver to store custom class instance.
class feedBack: NSObject, NSCoding {
var choiceA = 0
var choiceB = 0
var choiceC = 0
var choiceD = 0
var choiceNULL = 0
var sheetName = ""
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.choiceA, forKey: "choiceA")
aCoder.encodeObject(self.choiceB, forKey: "choiceB")
aCoder.encodeObject(self.choiceC, forKey: "choiceC")
aCoder.encodeObject(self.choiceD, forKey: "choiceD")
aCoder.encodeObject(self.choiceNULL, forKey: "choiceNULL")
aCoder.encodeObject(self.sheetName, forKey: "sheetName")
}
required init(coder aDecoder: NSCoder) {
self.choiceA = aDecoder.decodeObjectForKey("choiceA") as! Int
self.choiceB = aDecoder.decodeObjectForKey("choiceB") as! Int
self.choiceC = aDecoder.decodeObjectForKey("choiceC") as! Int
self.choiceD = aDecoder.decodeObjectForKey("choiceD") as! Int
self.choiceNULL = aDecoder.decodeObjectForKey("choiceNULL") as! Int
self.sheetName = aDecoder.decodeObjectForKey("sheetName") as! String
}
}
Before feedBack conforms to NSCoding, I use var fb = feedBack() to create a new instance of feedBack.
Now the compiler throws Missing argument for parameter coder in call error.
Since the initWithCoder is required, how do I call the previous initializer with no parameter?
Just override init() and that should do it.
class feedBack: NSObject, NSCoding {
var choiceA = 0
var choiceB = 0
var choiceC = 0
var choiceD = 0
var choiceNULL = 0
var sheetName = ""
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.choiceA, forKey: "choiceA")
aCoder.encodeObject(self.choiceB, forKey: "choiceB")
aCoder.encodeObject(self.choiceC, forKey: "choiceC")
aCoder.encodeObject(self.choiceD, forKey: "choiceD")
aCoder.encodeObject(self.choiceNULL, forKey: "choiceNULL")
aCoder.encodeObject(self.sheetName, forKey: "sheetName")
}
required init(coder aDecoder: NSCoder) {
self.choiceA = aDecoder.decodeObjectForKey("choiceA") as! Int
self.choiceB = aDecoder.decodeObjectForKey("choiceB") as! Int
self.choiceC = aDecoder.decodeObjectForKey("choiceC") as! Int
self.choiceD = aDecoder.decodeObjectForKey("choiceD") as! Int
self.choiceNULL = aDecoder.decodeObjectForKey("choiceNULL") as! Int
self.sheetName = aDecoder.decodeObjectForKey("sheetName") as! String
}
override init(){
}
}
feedBack()
I try to persiste my object with NSCoding but i always get BAD_ACCESS ERROR To avoid multi multiple like variable, class, i put all common variable in RObject. I think i do something wrong the the init but i don't know what.
the error was thow in this function
func parseInfo(allInfos : String) -> Void {
if let all : JSON = JSON.parse(allInfos) as JSON? {
if let info = all.asArray
{
for description in info
{
var track : RInfo = SCTracks(js: description)
self.arrayTracks.addObject(track)
} // Therad 1: EXC_BAD_ACCESS(code=2, address=0x27...)
}
}
}
The Log doesn't show any thing
My Common Class
class RObject : NSObject, NSCoding {
var id : Int? = 0
var kind : String?
override init() { super.init() }
init(js :JSON) {
self.kind = js["kind"].asString
self.id = js["id"].asInt
super.init()
}
required
init(coder aDecoder: NSCoder) {
self.id = aDecoder.decodeIntegerForKey("id") as Int
self.kind = aDecoder.decodeObjectForKey("kind") as? String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeInteger(self.id!, forKey: "id")
aCoder.encodeObject(self.kind, forKey: "kind")
}
}
My class Rinfo who inherits from RObject
class RInfo : RObject {
var title :String?
var uri :String?
var license :String?
var release :String?
var user :RUser!
override init() { super.init() }
required init(coder: NSCoder) {
self.title = coder.decodeObjectForKey("title") as? String
self.user = coder.decodeObjectForKey("user") as RUser
self.license = coder.decodeObjectForKey("license") as? String
self.uri = coder.decodeObjectForKey("uri") as? String
self.release = coder.decodeObjectForKey("release") as? String
super.init(coder: coder)
}
init(js :JSON) {
self.user = js(js: js["user"])
self.title = js["title"].asString
self.license = js["license"].asString
self.uri = js["uri"].asString
self.release = js["release"].asString
super.init(js: js)
}
override func encodeWithCoder(encoder: NSCoder) {
encoder.encodeObject(self.title, forKey: "title")
encoder.encodeObject(self.user, forKey: "user")
encoder.encodeObject(self.uri, forKey: "uri")
encoder.encodeObject(self.license, forKey: "license")
}
}
Thanks for any help !
I Solve my problem by remove variable release in RInfo. that strange