matehat / Objective-LevelDB

An Objective-C database library built over Google's LevelDB

Home Page:http://matehat.github.io/Objective-LevelDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift usage error about encodeWithCoder

hilen opened this issue · comments

I am using Objective-LevelDB in my Swift project, I implementation the encoder and decoder like this, there is nothing error with it.

self.levelDB.encoder = {(key: UnsafeMutablePointer<LevelDBKey>, object: ImplicitlyUnwrappedOptional<AnyObject>) -> NSData! in
     return NSKeyedArchiver.archivedDataWithRootObject(object)
} as LevelDBEncoderBlock

self.levelDB.decoder = {(key: UnsafeMutablePointer<LevelDBKey>, data: ImplicitlyUnwrappedOptional<NSData>) -> AnyObject! in
     return NSKeyedUnarchiver.unarchiveObjectWithData(data)!
} as! LevelDBDecoderBlock

But when I run this code, it crashes.

let object = NSObject()
self.levelDB.setObject(object, forKey: "key")

The error message is :

2016-01-13 19:41:15.157 LevelDBDemo[24442:9950819] -[NSObject encodeWithCoder:]: unrecognized selector sent to instance 0x7f968e0c28a0
2016-01-13 19:41:15.166 reason[24442:9950819] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSObject encodeWithCoder:]: unrecognized selector sent to instance 0x7f968e0c28a0'

How to deal with it?

My mistake, I should implementation NSCoding protocol.