nskeyedunarchiver

NSKeyedUnarchiver fails to decode a custom object in swift

只愿长相守 提交于 2019-12-21 12:42:40
问题 I'm trying a basic implementation of the NSCoding protocol in swift, but it seems I can't success to unarchive an object after it has been correctly archived. Here's my attempt import Cocoa class User: NSObject, NSCoding { var name: String init(name: String) { self.name = name } init(coder aDecoder: NSCoder!) { self.name = aDecoder.decodeObjectForKey("name") as String } func encodeWithCoder(aCoder: NSCoder!) { aCoder.encodeObject(name, forKey: "name") } } let user = User(name: "Gabriele") let

How can I decode an object when original class is not available?

半腔热情 提交于 2019-12-20 16:11:50
问题 I have an iOS 7 application that saves a custom object to app's iCloud Docs folder as a file. For this, I make use of NSCoding protocol. @interface Person : NSObject <NSCoding> @property (copy, nonatomic) NSString *name @property (copy, nonatomic) NSString *lastName @end Object serialization works perfectly in iOS 7 version of the app: initWithCoder and encodeWithCoder [NSKeyedArchiver archivedDataWithRootObject:person] person = NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)theData] But

How to implement the unarchiver delegate for overwriting the classname?

一笑奈何 提交于 2019-12-12 03:07:22
问题 I have 2 Targets of my IOS-App : MYAPPTARGET_1 & MYAPPTARGET_2 MYAPPTARGET_1 is writing a Stream to a BLOB using NSKeyedArchiver MYAPPTARGET_2 is reading a Stream from a BLOB using NSKeyedUnarchiver When unarchiving the Stream from the BLOB in my MYAPPTARGET_2, I get the Error: 016-01-18 15:01:38.541 WorldHistoryAtlasTest[598:9405] * Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of

NsUserDefualts not functioning right

穿精又带淫゛_ 提交于 2019-12-12 00:37:31
问题 Basically I have some sort of bug with tableView, i noticed that my tableView is not always getting updated correctly, and i tried debugging it and what i noticed is that tableView class is not always getting called to update the table. What am i doing wrong ? When i add new entry to my table, count 4 + 1, I go to history tab, and nothing happens and it shows as the count is still 4 but if I switch tabs 1 more time it will show count as 5 and tableView will be updated.. so there is a delay in

NSKeyedArchiver and sharing a custom class between targets

可紊 提交于 2019-12-11 08:55:19
问题 My app uses a custom class as its data model: class Drug: NSObject, NSCoding { // Properties, methods etc... } I have just created a Today extension and need to access the user’s data from it, so I use NSCoding to persist my data in both the app container and the shared container. These are the save and load functions in the main app: func saveDrugs() { // Save to app container let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(drugs, toFile: Drug.ArchiveURL.path) if isSuccessfulSave {

NSKeyedUnarchiver crashes swift 3 on the second run

 ̄綄美尐妖づ 提交于 2019-12-11 04:14:01
问题 I am trying to save custom object to UserDefaults and I'm using this as a source code. It crashes immediately on the get part. This is my code: class Settings { static let defaults:UserDefaults = UserDefaults.standard ///VIP object: class VIP: NSObject, NSCoding { let email: String let name: String let relation: String init(email: String, name: String, relation: String) { self.email = email self.name = name self.relation = relation } required init(coder decoder: NSCoder) { self.email =

Save/Get UIColor from UserDefaults

a 夏天 提交于 2019-12-10 21:56:34
问题 I need some help to load and read UIColor from UserDefaults. I found a nice extension to do that: extension UserDefaults { func colorForKey(key: String) -> UIColor? { var color: UIColor? if let colorData = data(forKey: key) { color = NSKeyedUnarchiver.unarchiveObject(with: colorData) as? UIColor } return color } func setColor(color: UIColor?, forKey key: String) { var colorData: NSData? if let color = color { colorData = NSKeyedArchiver.archivedData(withRootObject: color) as NSData? } set

Cannot decode object of class Employee for key (NS.object.0); the class may be defined in source code or a library that is not linked

喜欢而已 提交于 2019-12-05 14:01:54
问题 Im trying to pass an array of 'Employee' objects iPhone to Apple Watch by serializing the array : NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:employees]; and unserializing it as on the Watch side: NSMutableArray *employees = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject]; This is the 'Employee' class: @interface Employee : NSManagedObject @property (nonatomic, retain) NSNumber * employeeID; @property (nonatomic, retain) NSString * name; @property (nonatomic,

Load SpriteKit levels from filename

最后都变了- 提交于 2019-12-04 13:01:39
For a game I'm creating, I want to be able to create a lot of custom levels that can be loaded easily. Each level should have a .sks interface file and its own SKScene subclass .swift file. Right now, this is working: extension SKScene { class func unarchiveFromFile(file : NSString, theClass : AnyClass!) -> SKScene? { if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") { var sceneData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil) var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData) archiver.setClass(theClass, forClassName:

NSKeyedUnarchiver fails to decode a custom object in swift

故事扮演 提交于 2019-12-04 06:02:20
I'm trying a basic implementation of the NSCoding protocol in swift, but it seems I can't success to unarchive an object after it has been correctly archived. Here's my attempt import Cocoa class User: NSObject, NSCoding { var name: String init(name: String) { self.name = name } init(coder aDecoder: NSCoder!) { self.name = aDecoder.decodeObjectForKey("name") as String } func encodeWithCoder(aCoder: NSCoder!) { aCoder.encodeObject(name, forKey: "name") } } let user = User(name: "Gabriele") let encodedUser = NSKeyedArchiver.archivedDataWithRootObject(user) let decodedUser = NSKeyedUnarchiver