1

I have two arrays

var questions: [Question]!
var types: [[Type]]!

I am trying to convert those two arrays to NSData. I do like below

let typesData = NSKeyedArchiver.archivedDataWithRootObject(types)
let questionsData = NSKeyedArchiver.archivedDataWithRootObject(questions)

As soon as it hits typesData, the app crashes and throws an error like following:

*** NSForwarding: warning: object 0x15e5a4100 of class '(app Name).Type' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[(app Name).Type replacementObjectForKeyedArchiver:]

I am assuming it's causing an error since those arrays has custom types. Is there any way I can convert those arrays to NSData? Any solutions for solving this problem will be appreciated.

5
  • Do Question and Type implements initWithCoder:? Commented Jan 26, 2016 at 7:54
  • seems like u need to implements NSCoding for them then only u can save it as NSData Commented Jan 26, 2016 at 7:55
  • @Larme, they do not have initWithCoder function. Do I need to implement it? Commented Jan 26, 2016 at 7:57
  • @Tj3n, A bit more info would be appreciated Commented Jan 26, 2016 at 7:57
  • developer.apple.com/library/ios/documentation/Cocoa/Conceptual/… Commented Jan 26, 2016 at 7:58

1 Answer 1

1

For swift, you need to implement this in your Object (Question, Type) class like so:

class ItemObject:NSObject, NSCoding {
    var itemName = ""
    var itemCheck = false

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(itemName, forKey:"itemName")
        aCoder.encodeBool(itemCheck, forKey:"itemCheck")
    }


    required init (coder aDecoder: NSCoder) {
        self.itemName = aDecoder.decodeObjectForKey("itemName") as! String
        self.itemCheck = aDecoder.decodeBoolForKey("itemCheck")
    }

    override init() {
        super.init()
    }
}

This is also required for store custom object to NSUserDefaults and CoreData

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

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.