nscoding

NSUserDefaultsController: “Attempt to set a non-property-list object … as an NSUserDefaults/CFPreferences value for key …”

◇◆丶佛笑我妖孽 提交于 2019-12-25 01:09:01
问题 I'm trying to use NSUserDefaultsController to implement a Preferences window in my Swift macOS app. One of the settings that I need to persist is an array of presets, as defined by the following class: class Preset: NSObject { var name = "abc" var value = 123 override init() { super.init() } } Therefore, I need to persist a variable of type [Presets] . The visual representation in my Preferences window is an NSTableView , bound to an NSArrayController . I followed this tutorial to set up my

Swift NSCoding - How to read an encoded array

百般思念 提交于 2019-12-24 08:00:03
问题 I'm using Swift playground to write a parent-child relationship using NSCoding. The relationship can be described as: One Author can write many Books However, it causes an error when I try to save the collection of books; Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation. My

Archiving UIImageView with NSCoding

半世苍凉 提交于 2019-12-24 06:38:47
问题 I've never used NSCoding before and I'm very confused about how it should be implemented. My current iPad app has a UIImageView (called "background") which is a property of my main view controller. "background" has a UIImage "image" property (obviously) and various subviews which are added by the user. The added subviews are my own custom subclasses of UIImageView. I need to be able to save the state of the "background" UIImageView so it can be restored with the same image and all the

Advantages and disadvantages of encoding objects with NSCoding or simply writing data to files

被刻印的时光 ゝ 提交于 2019-12-24 02:27:11
问题 I'm curious what the advantages of encoding objects in objective c with NSCoding and writing them to disk may be over simply writing a persistence object to disk. Is there a performance increase in terms of I/O or disk space usage? 回答1: Well, most NSCoding implementations will handle object graphs correctly; i.e. if you code a member object that's already been coded to the coder, it won't code it again. Decoding will restore the object graph correctly (so the decoded target object has

Is it ok not to invoke [super init] in a custom init method?

拟墨画扇 提交于 2019-12-23 02:55:17
问题 I have a MKPolyline subblass which I want to implement NSCoding , i.e. @interface RSRoutePolyline : MKPolyline <NSCoding> I asked a question on the best way to encode the c-array and got an excellent answer. However, there is no init method defined on MKPolyline, i.e. there is no other way to give it data other than its class method polylineWithPoints:points . Is this code where my comment is ok? - (void)encodeWithCoder:(NSCoder *)aCoder { MKMapPoint *points = self.points; NSUInteger

How to archive enum with an associated value?

本秂侑毒 提交于 2019-12-23 02:52:49
问题 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

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

Strange behavoir when decoding an NSArray via NSSecureCoding

元气小坏坏 提交于 2019-12-21 12:05:20
问题 i spent all afternoon banging my head against the wall trying to figure out why decoding of this class was failing. the class has a property that is an NSArray of Foo objects. Foo conforms to NSSecureCoding, and i have successfully encoded and decoded that class by itself. i was getting an error in initWithCoder: that said failed to decode class Foo. through some experimentation, i discovered that i needed to add [Foo class] to initWithCoder: in order for it to work. maybe this will help

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 store an MKPolyline attribute as transformable in IOS coredata with swift?

给你一囗甜甜゛ 提交于 2019-12-19 06:04:14
问题 What would be the code required to allow the storage of an MKPolyline in CoreData in swift. So for example if I had one of my core data entities (say "myEntity") for which I wanted to save an MKPolyline, and have added the "polyline" field as transformable, and have set it to "transformable" in xcode. Also have produced NSManagedObject subclass. myEntity.swift import UIKit import CoreData import MapKit class myEntity: NSManagedObject { } myEntity+CoreDataProperties.swift import Foundation