nscoding

Encoding an Objective-c Block?

会有一股神秘感。 提交于 2019-11-30 08:17:18
Is it possible to encode an Objective-C block with an NSKeyedArchiver ? I don't think a Block object is NSCoding -compliant, therefore [coder encodeObject:block forKey:@"block"] does not work? Any ideas? bbum No, it isn't possible for a variety of reasons. The data contained within a block isn't represented in any way similar to, say, instance variables. There is no inventory of state and, thus, no way to enumerate the state for archival purposes. Instead, I would suggest you create a simple class to hold your data, instances of which carry the state used by the blocks during processing and

Archiving / Unarchiving results in initForReadingWithData incomprehensible archive

若如初见. 提交于 2019-11-30 07:39:46
问题 I've implemented an save on applicationWillTerminate and load on applicationWillFinishLoading . There is a complete object tree, all implement the NSCoding protocol and I've check the types I enter. One of the classes also stores an NSMutableData to the NSKeyedArchive , which I suspect might break unarchiving occasionally. Weirdly enough, sometimes it works and sometimes it doesn't. I suspect some content in the NSMutableData will break the archiving. I use encodeObject on all objects, except

Save struct in class to NSUserDefaults using Swift

♀尐吖头ヾ 提交于 2019-11-30 07:19:05
I have a class and inside the class is a (swift) array, based on a global struct. I want to save an array with this class to NSUserDefaults. This is my code: struct mystruct { var start : NSDate = NSDate() var stop : NSDate = NSDate() } class MyClass : NSObject { var mystructs : [mystruct] init(mystructs : [mystruct]) { self.mystructs = mystructs super.init() } func encodeWithCoder(encoder: NSCoder) { //let val = mystructs.map { $0 as NSObject } //this also doesn't work let objctvtmrec = NSMutableArray(mystructs) //gives error encoder.encodeObject(objctvtmrec) //first approach: encoder

Swift call class function from corresponding subclass in superclass function

余生长醉 提交于 2019-11-30 06:59:56
问题 I would like to implement init(coder aDecoder: NSCoder!) in a superclass, and use it in all subclasses by calling a class method on the particular subclass in the superclass at runtime. MySuperClass class func dummyDict() -> NSDictionary init(coder aDecoder: NSCoder!) { for(key,value) in self.class.dummyDict(){ -------------------- ^ | | Get this from the corresponding subclass at runtime! NSLog("encoding \(value) for key \(key)") } } Is it possible that subclasses from MySuperClass access

iPhone - Why does the documentation say UIImageView is NSCoding compliant?

醉酒当歌 提交于 2019-11-30 06:27:36
问题 Ideally an NSCoding compliant class will work as expected using encodeWithCoder: and initWithCoder: (at least I thought so till recently) without the developer having to bother about what goes on inside the routines (unless my idea of an NSCoding compliant class are totally screwed up!) The UIImageView class is NSCoding compliant. So I should not have to bother how it will be serialized/de-serialized using the NSKeyedArchiver and NSKeyedUnarchiver classes. But every time I try and encode a

objects conforming to nscoding will not writetofile

99封情书 提交于 2019-11-29 15:23:59
I want to write an array of Question objects to file, but somehow writeToFile isn't doing anything. A Question has an Owner and and an array of Answer objects. An Answer has an Owner as well. All three of them conform to the NSCoding protocol (as far as I know). From the code below, result returns NO. No clue what I'm doing wrong, since I am implementing NSCoding for everything, right? Question.h #import <Foundation/Foundation.h> #import "Owner.h" @interface Question : NSObject <NSCoding> { NSString *questionId; NSString *questionRevision; NSString *text; NSDate *date; NSMutableArray *answers;

How can I get an NSCoder to encode/decode a Swift array of structs?

大憨熊 提交于 2019-11-29 12:53:13
I have an object that must conform to NSCoding and that holds an array of UInt64 values. How can I encode/decode it with an NSCoder at all? Bonus question: how can I encode it most compactly? (It has to go into saved Game Center state data, whose size is limited.) Ideally, I just want to write an Int which is the size n of the array, and then write n times the 64 bits of a UInt64 , and read it similarly. Can I do this? coder.encodeObject(values, forKey: "v") doesn't work. class MyObject: NSCoding { private var values: [UInt64] // … // MARK: - NSCoding required init(coder decoder: NSCoder) { //

Encoding an Objective-c Block?

梦想与她 提交于 2019-11-29 11:26:25
问题 Is it possible to encode an Objective-C block with an NSKeyedArchiver ? I don't think a Block object is NSCoding -compliant, therefore [coder encodeObject:block forKey:@"block"] does not work? Any ideas? 回答1: No, it isn't possible for a variety of reasons. The data contained within a block isn't represented in any way similar to, say, instance variables. There is no inventory of state and, thus, no way to enumerate the state for archival purposes. Instead, I would suggest you create a simple

Converting CLLocationCoordinate2D to a String that can be stored

寵の児 提交于 2019-11-29 11:23:40
I'm trying to save the coordinates of a user while in one ViewController so that it can be used to create an Annotation that can displayed in another ViewController. In the view controller that stores the coordinates I'm using the code NSUserDefaults.standardUserDefaults().setObject( Location, forKey: "Location") In the map view controller that displays the annotation I'm trying to get the coordinates using the code let Location = NSUserDefaults.standardUserDefaults().stringForKey("Location") var Annotation = MKPointAnnotation() Annotation.coordinate = Location It is telling me that the value

Swift call class function from corresponding subclass in superclass function

隐身守侯 提交于 2019-11-28 23:55:14
I would like to implement init(coder aDecoder: NSCoder!) in a superclass, and use it in all subclasses by calling a class method on the particular subclass in the superclass at runtime. MySuperClass class func dummyDict() -> NSDictionary init(coder aDecoder: NSCoder!) { for(key,value) in self.class.dummyDict(){ -------------------- ^ | | Get this from the corresponding subclass at runtime! NSLog("encoding \(value) for key \(key)") } } Is it possible that subclasses from MySuperClass access the class function dummyDict() at runtime ? I think I caught what you mean. You create a Base class,