0

I have a swift class integrated in my objective-c project and it returns me an object that I can access and use. I've tried (in an obj-c file) to create a property with this object and encode/decode it using standard methods

- (void)encodeWithCoder:(NSCoder *)encoder

and

- (id)initWithCoder:(NSCoder *)decoder

But my application crashes for "unrecognized selector sent to instance XXX".

I guess I can't serialize swift objects in this direct way, and I don't know how to solve right now. Thank you for your help

3
  • 1
    Please you can write your class (more code) to be detected the error? Commented Mar 29, 2016 at 9:52
  • I don't know what code to post, I try to serialize the swift object as usual.. [encoder encodeObject:self.OBAds forKey:@"OBAds"]; Commented Mar 29, 2016 at 10:13
  • It seems to me that the OBAds object doesn't implement NSCoder Commented Mar 29, 2016 at 10:37

2 Answers 2

1

In order for this to work you will have to:

a) write the set of encodeWithCoder methods in your Swift class

-or-

b) inherit from an Obj-C class that does

(b) caused me confusion until the simple truth became clear: Swift objects do not descend from NSObject, so a lot of the machinery you expect simply isn't there, and that includes the entire coder system. Quite a bit of functionality becomes available when you change:

class myClass {

to

class myClass: NSObject {
Sign up to request clarification or add additional context in comments.

Comments

0

This is a simple class swift. You may take this as an example. I hope we can be of your help.

    class UserBean:NSObject,NSCoding {

    var idUser:NSNumber?
    var username:String?
    var password:String?
    var email:String?
    var token:String?
    var fonte:String?
    var idFacebook:String?
    var uuid:String?
    var profilo = UserProfileBean()

    override init() {
        super.init()
        self.profilo = UserProfileBean()
    }

    required init?(coder aDecoder: NSCoder) {
        idUser      = aDecoder.decodeObjectForKey("idUser") as? NSNumber
        username    = aDecoder.decodeObjectForKey("username") as? String
        password    = aDecoder.decodeObjectForKey("password") as? String
        email       = aDecoder.decodeObjectForKey("email") as? String
        token       = aDecoder.decodeObjectForKey("token") as? String
        fonte       = aDecoder.decodeObjectForKey("fonte") as? String
        idFacebook  = aDecoder.decodeObjectForKey("idFacebook") as? String
        uuid        = (aDecoder.decodeObjectForKey("uuid") as? String)!
        profilo     = (aDecoder.decodeObjectForKey("profilo") as? UserProfileBean)!

    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(idUser,     forKey: "idUser")
        aCoder.encodeObject(username,   forKey: "username")
        aCoder.encodeObject(password,   forKey: "password")
        aCoder.encodeObject(email,      forKey: "email")
        aCoder.encodeObject(token,      forKey: "token")
        aCoder.encodeObject(fonte,      forKey: "fonte")
        aCoder.encodeObject(idFacebook ,forKey: "idFacebook")
        aCoder.encodeObject(uuid ,forKey: "idFacebook")
        aCoder.encodeObject(profilo,forKey: "profilo")

    }  
  }

Comments

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.