nscoder

Simple Swift class does not compile

对着背影说爱祢 提交于 2019-12-01 21:10:31
My simple class, ClassWithOneArray, produces this error: Bitcast requires both operands to be pointer or neither %19 = bitcast i64 %18 to %objc_object*, !dbg !470 LLVM ERROR: Broken function found, compilation aborted! Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1 However, my class, ClassWithOneInt, does not. Why? class ClassWithOneInt { var myInt = Int() init(myInt: Int) { self.myInt = Int() } func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeObject(myInt, forKey: "myInt") } init(coder aDecoder: NSCoder) {

Encoding NSAttributedString Throws Error

ぐ巨炮叔叔 提交于 2019-12-01 12:28:08
问题 Based on the accepted answer to this question I wrote the following code: NSData* somedata; somedata=[NSKeyedArchiver archivedDataWithRootObject:ts]; where ts is an NSAttributedString that is populated with some text and some attributes (colors, in this case). When I execute this code, I receive this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x6eb5b90' I'm new to the

Can NSManagedObject conform to NSCoding

笑着哭i 提交于 2019-11-30 20:44:31
I need to transfer a single object across device. Right now I am converting my NSManagedObject to a dictionary , archiving it and sending as NSData. Upon receiving I am unarchiving it. But I would really like to transfer the NSManagedObject itself by archiving and unarchiving instead of creating an intermediate data object. @interface Test : NSManagedObject<NSCoding> @property (nonatomic, retain) NSString * title; @end @implementation Test @dynamic title; - (id)initWithCoder:(NSCoder *)coder { self = [super init]; if (self) { self.title = [coder decodeObjectForKey:@"title"]; //<CRASH } return

Objective C - How do I use initWithCoder method?

为君一笑 提交于 2019-11-27 11:53:25
I have the following method for my class which intends to load a nib file and instantiate the object: - (id)initWithCoder:(NSCoder*)aDecoder { if(self = [super initWithCoder:aDecoder]) { // Do something } return self; } How does one instantiate an object of this class? What is this NSCoder ? How can I create it? MyClass *class = [[MyClass alloc] initWithCoder:aCoder]; SegFault You also need to define the following method as follows: - (void)encodeWithCoder:(NSCoder *)enCoder { [super encodeWithCoder:enCoder]; [enCoder encodeObject:instanceVariable forKey:INSTANCEVARIABLE_KEY]; // Similarly for

How do I encode enum using NSCoder in swift?

六眼飞鱼酱① 提交于 2019-11-26 17:39:16
Background I am trying to encode a String-style enum using the NSCoding protocol, but I am running into errors converting to and back from String. I get the following errors while decoding and encoding: String is not convertible to Stage Extra argument ForKey: in call Code enum Stage : String { case DisplayAll = "Display All" case HideQuarter = "Hide Quarter" case HideHalf = "Hide Half" case HideTwoThirds = "Hide Two Thirds" case HideAll = "Hide All" } class AppState : NSCoding, NSObject { var idx = 0 var stage = Stage.DisplayAll override init() {} required init(coder aDecoder: NSCoder) { self

Objective C - How do I use initWithCoder method?

十年热恋 提交于 2019-11-26 15:47:35
问题 I have the following method for my class which intends to load a nib file and instantiate the object: - (id)initWithCoder:(NSCoder*)aDecoder { if(self = [super initWithCoder:aDecoder]) { // Do something } return self; } How does one instantiate an object of this class? What is this NSCoder ? How can I create it? MyClass *class = [[MyClass alloc] initWithCoder:aCoder]; 回答1: You also need to define the following method as follows: - (void)encodeWithCoder:(NSCoder *)enCoder { [super