nscoder

How to encode/decode a long long property with NSCoder?

試著忘記壹切 提交于 2019-12-07 00:30:08
问题 How to decode a long long type property in initWithCoder: and encode in encodeWithCoder: with NSCoder in iOS? Thanks. 回答1: On iOS a long long is 64 bit, so use the encodeInt64: forKey: and decodeInt64ForKey: methods. 来源: https://stackoverflow.com/questions/13486612/how-to-encode-decode-a-long-long-property-with-nscoder

Swift - Why init(coder) is required in AFHTTPSessionManager?

天大地大妈咪最大 提交于 2019-12-06 08:58:40
I'm not very experienced in iOS development. While making subclass of AFHTTPSessionManager XCode suggested me to include required init(coder) : import UIKit let _sharedAPIManager = APIManager(baseURL: NSURL(string: API_URL)!) class APIManager: AFHTTPSessionManager { /** * Singleton service * (https://github.com/hpique/SwiftSingleton) */ class var sharedInstance : APIManager { return _sharedAPIManager } init(baseURL url: NSURL!) { super.init(baseURL: url, sessionConfiguration: nil) self.responseSerializer = AFJSONResponseSerializer() as AFJSONResponseSerializer self.requestSerializer =

When does encodeWithCoder get called?

不羁岁月 提交于 2019-12-06 02:15:40
问题 This will save an array (I think). - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:myArray forKey:@"myArray"]; } Do I have to directly call this method when I want to save an array or do I have to do something else? 回答1: You don’t call this method directly. It’s called by a NSCoder subclass if it needs to serialize that object. If you want to encode an object graph use the class methods archivedDataWithRootObject: or archiveRootObject:toFile: of NSKeyedArchiver . This in

NSCoder and custom types

北城以北 提交于 2019-12-05 04:54:14
How do you use NSCoder to encode and decode custom types? For example, how would you use NSCoder with an instance of " STATE " where: typedef enum { ON, OFF } STATE; You can treat them as integers as they are implicitly assigned integer values: - (void) encodeWithCoder: (NSCoder *)coder { ... [coder encodeInt:type forKey:@"state"]; } - (id) initWithCoder: (NSCoder *)coder { ... state = [coder decodeIntForKey:@"state"]; } 来源: https://stackoverflow.com/questions/4029760/nscoder-and-custom-types

When does encodeWithCoder get called?

一个人想着一个人 提交于 2019-12-04 08:46:20
This will save an array (I think). - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:myArray forKey:@"myArray"]; } Do I have to directly call this method when I want to save an array or do I have to do something else? You don’t call this method directly. It’s called by a NSCoder subclass if it needs to serialize that object. If you want to encode an object graph use the class methods archivedDataWithRootObject: or archiveRootObject:toFile: of NSKeyedArchiver . This in turn will call the encodeWithCoder: method of your objects. Also note that every object in your array has to

NSCoder: Unrecognized selector sent to instance

一笑奈何 提交于 2019-12-02 09:28:34
So, I'm new to iOS and have been using online tutorials to learn the platform. The tutorials I'm using are building apps with iOS 9 while I'm using iOS 10. Because of that I have run into the title issue. Here is some code: CalendarEvent.swift class CalendarEvent : NSObject { var title : String var dateString : String init(withTitle t : String, andDateString ds : String) { title = t dateString = ds } init(withCoder coder : NSCoder) { dateString = coder.decodeObject(forKey: "dateString") as! String title = coder.decodeObject(forKey: "title") as! String } func encodeWithCoder(coder : NSCoder) {

Unarchiving UIImageView with subviews

倾然丶 夕夏残阳落幕 提交于 2019-12-02 05:12:12
问题 I'm going a bit crazy trying to archive and unarchive a UIImageView which has number of subviews which are custom views derived from UIImageView. Here's what I've done: Add a category to the project to allow for the fact that UIImage does not conform to NSCoding: #import "UIImage-NSCoding.h" #define kEncodingKey @"UIImage" @implementation UIImage(NSCoding) - (id)initWithCoder:(NSCoder *)decoder { if ((self = [super init])) { NSData *data = [decoder decodeObjectForKey:kEncodingKey]; self =

Swift Save viewcontroller state using coder beyond app close

亡梦爱人 提交于 2019-12-02 00:40:08
I am using Xcode 8 and swift 2.3 I want to save the entire view controller to file and restore state even after app closes. I searched everywhere and found we need to use coder for that. but all just shows to save an object. but here I need to save entire ViewContoller and subviews. ViewCotroller will have three buttons Add Text Add Image : User can add any number of textViews and Images. So I need to save all that info also. Add ViewController : User may have an array of this viewController and need to save all. Question 1) Can just save self.view and can it save all subviews automatically ?

Unarchiving UIImageView with subviews

为君一笑 提交于 2019-12-01 23:26:15
I'm going a bit crazy trying to archive and unarchive a UIImageView which has number of subviews which are custom views derived from UIImageView. Here's what I've done: Add a category to the project to allow for the fact that UIImage does not conform to NSCoding: #import "UIImage-NSCoding.h" #define kEncodingKey @"UIImage" @implementation UIImage(NSCoding) - (id)initWithCoder:(NSCoder *)decoder { if ((self = [super init])) { NSData *data = [decoder decodeObjectForKey:kEncodingKey]; self = [self initWithData:data]; } return self; } - (void)encodeWithCoder:(NSCoder *)encoder { NSData *data =

Is there a way to encode and decode the closures?

妖精的绣舞 提交于 2019-12-01 23:15:37
问题 For some reason, I need to serialize a class object that contains several closure fields. Like this: import Foundation class Foo: NSObject, NSCoding { var bar: (() -> Void) override init() { bar = {} } required init(coder aDecoder: NSCoder) { bar = aDecoder.decodeObject(forKey: "bar") as! (() -> Void) } func encode(with aCoder: NSCoder) { aCoder.encode(bar, forKey: "bar") } } let foo = Foo() foo.bar = { print("Help!") } let data = NSKeyedArchiver.archivedData(withRootObject: foo) let object =