nscoding

How to archive enum with an associated value?

左心房为你撑大大i 提交于 2019-12-06 15:20:03
I'm trying to encode an object and i have some troubles. It work's fine with strings, booleans and else, but i don't know how to use it for enum. I need to encode this: enum Creature: Equatable { enum UnicornColor { case yellow, pink, white } case unicorn(UnicornColor) case crusty case shark case dragon I'm using this code for encode: func saveFavCreature(creature: Dream.Creature) { let filename = NSHomeDirectory().appending("/Documents/favCreature.bin") NSKeyedArchiver.archiveRootObject(creature, toFile: filename) } func loadFavCreature() -> Dream.Creature { let filename = NSHomeDirectory()

Intermittent data loss with background fetch - could NSKeyedUnarchiver return nil from the documents directory?

╄→尐↘猪︶ㄣ 提交于 2019-12-06 13:51:36
问题 I have a simple app that stores an array of my custom type (instances of a class called Drug ) using NSCoding in the app’s documents folder. The loading and saving code is an extension to my main view controller, which always exists once it is loaded. Initialisation of array: var drugs = [Drug]() This array is then appended with the result of the loadDrugs() method below. func saveDrugs() { // Save to app container let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(drugs, toFile: Drug

Best practice of copying and serializing Quartz references

孤街醉人 提交于 2019-12-06 11:21:54
问题 I have objects containing Quartz-2D references (describing colors, fill patterns, gradients and shadows) in Cocoa. I would like to implement the NSCoding protocol in my objects and thus need to serialize those opaque Quartz-2D structures. Possible solutions might be: Define a set of properties in my objects that allow to setup the data structures from scratch whenever they are needed. Those can then be serialized easily. Example: Store four floats for red, green, blue, and alpha, then use

CGPathRef encoding

主宰稳场 提交于 2019-12-06 08:44:45
问题 Is it possible to encode CGPathRef variables? I mean is there an encodeObject:forKey like method for CGPathRef variables? 回答1: yes, there is - I have done this: https://github.com/mro/MROGeometry/blob/master/NSCoder_MROCGPath.m 回答2: CGPaths are objects, but I don't think you can encode them—at least, if you can, it's not documented. You'll need to generate a saveable representation of the path yourself, and encode that, and then, when you decode that representation, reincarnate the path from

How to encode and decode a custom class with NSKeyedArchiver

丶灬走出姿态 提交于 2019-12-06 01:06:01
问题 I have a custom class that I wish to save and load. The class contains an NSDate, an NSString, and an NSNumber. I have implemented the NSCoding protocol in the .h file. Here is the code I have so far. theDate is an NSDate. theName is the NSString. homeAway is the NSNumber. -(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:theDate forKey:@"theDate"]; [aCoder encodeObject:theName forKey:@"theName"]; [aCoder encodeObject:homeAway forKey:@"homeAway"]; } -(id)initWithCoder:(NSCoder *

Swift enum and NSCoding

☆樱花仙子☆ 提交于 2019-12-05 17:39:41
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 data I wish to store is thing1.directions: Direction = [.North("thing2"), .South("thing3")] (In a more

Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject [duplicate]

橙三吉。 提交于 2019-12-05 13:25:18
问题 This question already has answers here : NSObject subclass in Swift: hash vs hashValue, isEqual vs == (2 answers) Closed 3 years ago . The FooBar class below has to override the == function of the Equatable type. However, calling contains on an array of FooBar objects does not cause a breakpoint inside the custom == function to get invoked. Is it possible another == function is overriding this custom one? Note: Because FooBar must subclass from NSCoding and NSObject, FooBar does not list

Saving PFObject NSCoding

狂风中的少年 提交于 2019-12-04 18:33:54
问题 My Problem: saveInBackground isn't working. The Reason It's not working: I'm saving PFObjects stored in an NSArray to file using NSKeyedArchiving . The way I do that is by implementing NSCoding via this library. For some reason unknown to me, several other fields are being added and are set to NULL. I have a feeling that this is screwing up the API call to saveInBackground . When I call saveInBackground on the first set of objects (before NSKeyedArchiving ) saveInBackground works just fine.

CGPathRef encoding

让人想犯罪 __ 提交于 2019-12-04 13:05:54
Is it possible to encode CGPathRef variables? I mean is there an encodeObject:forKey like method for CGPathRef variables? yes, there is - I have done this: https://github.com/mro/MROGeometry/blob/master/NSCoder_MROCGPath.m CGPaths are objects, but I don't think you can encode them—at least, if you can, it's not documented. You'll need to generate a saveable representation of the path yourself, and encode that, and then, when you decode that representation, reincarnate the path from it yourself. This header on Github has two utility strings to interchange CGPaths with NSStrings: +(nullable

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