nscoding

NSKeyedUnarchiver.unarchiveTopLevelObjectWithData return nil

房东的猫 提交于 2021-02-11 15:14:16
问题 I want to save an array of objects into UserDefaults and load it back. When trying to unarchive the data it always returns nil.. any idea? This is my object: class DbInsideLevel: NSObject, NSSecureCoding { static var supportsSecureCoding: Bool { return true } let idNum: Int! var topicId: Int = 0 var tryCount: Int = 0 var score: Int = 0 var isOpen: Bool = false var lastPlayedDate: Date? init(idNum: Int, topicId: Int, tryCount: Int = 0, score: Int = 0, open: Bool, lastPlayedDate: Date?) { self

NSKeyedUnarchiver decodeObjectForKey: cannot decode object of class for key (NS.objects)

[亡魂溺海] 提交于 2021-02-10 07:06:57
问题 I looked through whole SO but still no answer. My app reports this problem: Fatal Exception: NSInvalidUnarchiveOperationException *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (App_Title.Products) for key (NS.objects); the class may be defined in source code or a library that is not linked I did already do NSKeyedUnarchiver.setClass just before #unarchiveObject: func loadProducts() -> [Products]? { NSKeyedUnarchiver.setClass(Products.self, forClassName: "Products

Swift enum and NSCoding

℡╲_俬逩灬. 提交于 2020-01-23 08:26:29
问题 I have a 'Thing' object with a String property and an NSImage property; the Thing class has encodeWithCoder: and decodeWithCoder: methods, and I can archive and unarchive a [Thing] array using NSKeyedArchiver/Unarchiver. So far, so good. Now I want to expand my Thing class by an array of directions, where 'Direction' is the following enum: enum Direction{ case North(direction:String) case East(direction:String) case South(direction:String) case West(direction:String) } In other words, the

How to convert NSValue to NSData and back?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-10 10:14:35
问题 A have a number of NSValue (obtained via KVC valueForKey ) that I need to append to an NSData object in order to send it over the network using Game Center. Obviously I will also need to convert the NSValue back from NSData for more KVC ( setValue:forKey: ). I don't know the exact type of each NSValue , so I'm limited to using the interfaces provided by NSValue and NSData . I should mention that the NSValue are never pointer types. I'm surprised that there's no [NSData dataWithValue:value]

How to save and load apps last data+view? (without NSCoding?)

天涯浪子 提交于 2020-01-06 19:52:52
问题 I am using Swift 2 and Xcode 7 for iOS 9.2 My, basically finished, game-app creates a complex view ( SceneKit + SpriteKit + random variables + game progress). When I switch to the Menu and back, everything stays the same (just switching views or scenes). However, when the application is closed, it restarts with the main.storyboard's entry-point and my data is gone. The app should open the same state it was closed on. I already tried archiving relevant data with NSCoding to save/load the scene

Saving an NSArray of custom objects

一世执手 提交于 2019-12-30 11:19:06
问题 I've created a subclass of UIImage (UIImageExtra) as I want to include extra properties and methods. I have an array that contains instances of this custom class.However when I save the array, it appears the extra data in the UIImageExtra class is not saved. UIImageExtra conforms to NSCoding, but neither initWithCoder or encodeWithCoder are called, as NSLog statements I've added aren't printed. My method to save the array looks like this: - (void)saveIllustrations { if (_illustrations == nil)

NSSecureCoding trouble with collections of custom class

懵懂的女人 提交于 2019-12-30 03:20:07
问题 I am having trouble with adopting NSSecureCoding. I encode an array containing objects of my custom class, which adopts NSSecureCoding properly. When I decode it, passing the class NSArray (which is the class of the object I encoded), it throws an exception. However, when do the exact same thing with an array of strings, it works fine. I fail to see what is the difference between my class and NSString. #import <Foundation/Foundation.h> @interface Foo : NSObject <NSSecureCoding> @end

How to encode a value passed through a delegate using NSCoding

最后都变了- 提交于 2019-12-25 08:08:34
问题 I'm really new using the NSCoding functionality, I'm trying to create persistence of data of an attributedString, particularly a UIColor that is passed through a delegate. I haven't found a tutorial that encodes values that are not declared and initialized in the same class in which the NSCoding protocol is conformed. I have the following code, which is the method that conforms to the protocol I created, and assigns the passed color value as an attribute to the attributedString. func

'self' used before self.init call error while using NSCoding on a custom class

强颜欢笑 提交于 2019-12-25 07:19:28
问题 I'm trying to encode a custom class so I can save it using NSKeyedArchiver.archivedDataWithRootObject but when I try to conform to the NSCoding protocol, I get this error : 'self' used before self.init. Here is my code: class MemberRewardsInfo: NSObject, NSCoding { var id: Int? required convenience init?(coder aDecoder: NSCoder) { guard let unarchivedId = aDecoder.decodeObjectForKey("id") as? Int else { return nil } } func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeObject(id, forKey: "id

Using NSCoder and NSKeyedArchiver with runtime reflection to deep copy a custom class

僤鯓⒐⒋嵵緔 提交于 2019-12-25 04:16:05
问题 I'd like to use - (id)initWithCoder:(NSCoder *)coder and - (void)encodeWithCoder:(NSCoder *)coder to encode a custom class for copying (using NSKeyedArchiver etc) My custom class contains a large number of iVars. Instead of listing all of these in the encoding code I'd like to use reflection to simply enumerate over all the properties in the custom class and encode/decode each property from within a loop. I'll end up with code that looks a bit like this: - (id)initWithCoder:(NSCoder *)coder {