6

i'm newbie in swift adn i have problem with initwithcoder in swift.

I have class UserItem, i need it to save user login.

in objective c is like this

 - (id)initWithCoder:(NSCoder *)decoder{
    if (self = [super init]){
        self.username = [decoder decodeObjectForKey:@"username"];
    }
    return self;
}

and in swift i'm trying like this

override init() {
   super.init()
}    

required init(coder decoder: NSCoder!) {

   self.username = (decoder.decodeObjectForKey("username")?.stringValue)!

   super.init(coder: decoder)
}

but if like above, i get error on code

super.init(coder: decoder)

error message is "extra argument 'coder' in call

i can't figure out anymore, so i'm try this code,

convenience init(decoder: NSCoder) {
   self.init()

   self.username = (decoder.decodeObjectForKey("username")?.stringValue)!
}

but, get error

.UserItem initWithCoder:]: unrecognized selector sent to instance 0x7fd4714ce010

what should i do? thanks before for your help.

1
  • 2
    "have class UserItem" Show its declaration, please. Commented Nov 30, 2015 at 2:42

1 Answer 1

14

I struggled with NSCoding (the protocol that you use to archive and unarchive objects) in the past and I'm seeing that you are going through the same pains. Hope this lessen it a bit:

class UserItem: NSObject, NSCoding {
    var username: String
    var anInt: Int

    init(username: String, anInt: Int) {
        self.username = username
        self.anInt = anInt
    }

    required init?(coder aDecoder: NSCoder) {
        // super.init(coder:) is optional, see notes below
        self.username = aDecoder.decodeObjectForKey("username") as! String
        self.anInt = aDecoder.decodeIntegerForKey("anInt")
    }

    func encodeWithCoder(aCoder: NSCoder) {
        // super.encodeWithCoder(aCoder) is optional, see notes below
        aCoder.encodeObject(self.username, forKey: "username")
        aCoder.encodeInteger(self.anInt, forKey: "anInt")
    }

    // Provide some debug info
    override var description: String {
        get {
            return ("\(self.username), \(self.anInt)")
        }
    }
}

// Original object
let a = UserItem(username: "michael", anInt: 42)

// Serialized data
let data = NSKeyedArchiver.archivedDataWithRootObject(a)

// Unarchived from data
let b = NSKeyedUnarchiver.unarchiveObjectWithData(data)!

print(a)
print(b)

The important thing is to match up the keys and the data types in encodeWithCoder(aCoder:) (the archive function) and init(coder:) (the unarchive function).

Where is confusing for beginners is what to do with the superclass. You should only include the superclass in the two functions above if the superclass itself conforms to NSCoding. NSObject does not provide that by itself. The idea is each class knows about its own properties, some of which are private. If the superclass cannot archive/unarchive, there's no need to call them.

Sign up to request clarification or add additional context in comments.

3 Comments

How to invoke Base/Abstract UIViewController from Nib file?
I have a similar code, but in my case, the compiler asks me to add "convenience" to required init?(coder aDecoder: NSCoder). Which prevents me form calling super.(init: coder). Did I miss something?
shouldn't you call "super.init()" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.