nskeyedarchiver

Saving CLLocation error: Mutating method sent to immutable object

爱⌒轻易说出口 提交于 2019-12-08 05:06:43
问题 I have read the other related questions, but I am stuck. I am trying to save the last known location into a plist for later use. Here is the error message I am receiving: * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' Here is my code: var plist = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("Config", ofType: "plist")) var dataToStore =

Saving CLLocation error: Mutating method sent to immutable object

限于喜欢 提交于 2019-12-07 17:57:31
I have read the other related questions, but I am stuck. I am trying to save the last known location into a plist for later use. Here is the error message I am receiving: * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' Here is my code: var plist = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("Config", ofType: "plist")) var dataToStore = NSKeyedArchiver.archivedDataWithRootObject(lastKnownLocation) plist.setValue(dataToStore, forKey: "location") The

NSKeyedArchiver archivedDataWithRootObject:

可紊 提交于 2019-12-07 02:56:31
问题 Is the parameter for NSKeyedArchiver archivedDataWithRootObject: supposed to be the array I am trying to save, or the array converted into NSData? 回答1: To convert a generic array to an NSData , you need an archiver! If you know how to feed the NSData , you know how to use NSKeyedArchiver . So: NSArray* array= ... ; NSData* data=[NSKeyedArchiver archivedDataWithRootObject:array]; Of course all elements in your array needs to implement encodeWithCoder: . 回答2: Yuji's answer is right. but more

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 *

UserDefaults/KeyedArchiver Frustrations

拜拜、爱过 提交于 2019-12-05 20:57:52
I'm working on a homework app that uses custom Assignment objects for each assignment. I am trying to store an NSMutableArray (casted to an NSArray via initWithArray:) in standardUserDefaults but I'm having trouble with saving and reloading the array. I have a table view from which you can choose to add a new assignment (which loads NewAssignmentViewController). When you save the assignment, it is pushed back to an array in AssigmentsViewController. And then you call it every time you load the UITableView which shows the assignments. Here is the relating code: -(void)saveToUserDefaults:

Cannot decode object of class Employee for key (NS.object.0); the class may be defined in source code or a library that is not linked

喜欢而已 提交于 2019-12-05 14:01:54
问题 Im trying to pass an array of 'Employee' objects iPhone to Apple Watch by serializing the array : NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:employees]; and unserializing it as on the Watch side: NSMutableArray *employees = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject]; This is the 'Employee' class: @interface Employee : NSManagedObject @property (nonatomic, retain) NSNumber * employeeID; @property (nonatomic, retain) NSString * name; @property (nonatomic,

iOS数据持久化之二——归档与设计可存储化的数据模型基类

血红的双手。 提交于 2019-12-05 10:23:25
iOS数据持久化之二——归档与设计可存储化的数据模型基类 一、引言 在上一篇博客中,我们介绍了用plist文件进行数据持久化的方法。虽然简单易用,但随着开发的深入,你会发现,这种方式还是有很大的局限性。试想,如果我们可以将用户的登录返回信息模型,游戏中角色的属性信息模型进行直接的持久化存取,那是不是非常爽的事,幸运的是,我们可以通过归档,来设计一个这样的数据模型。 二、先来精通归档吧 归档也是iOS提供给开发者的一种数据存储的方式,事实上,几乎所有的数据类型都可以通过归档来进行存取。其存储与读取的过程,主要封装在两个类中:NSKeyedArchiver和NSKeyedUnarchiver。 1、归档的原理 归档是将一种或者多种数据类型进行序列化,解归档的过程就是将序列化的数据进行反序列化的解码,这里需要注意一点,归档的核心并非是数据的持久化处理,而是数据的序列化处理,持久化的处理依然是通过文件存取来实现的。因此,被归档的数据类型都必须遵守一个相同的协议,才能在这个协议的约束下进行正确的归档与解归档,这个协议就是NSCoding协议,我们可以先来看一下NSCoding中的内容: @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; - (id)initWithCoder:(NSCoder *)aDecoder;

NSKeyedArchiver archivedDataWithRootObject:

妖精的绣舞 提交于 2019-12-05 08:07:49
Is the parameter for NSKeyedArchiver archivedDataWithRootObject: supposed to be the array I am trying to save, or the array converted into NSData? To convert a generic array to an NSData , you need an archiver! If you know how to feed the NSData , you know how to use NSKeyedArchiver . So: NSArray* array= ... ; NSData* data=[NSKeyedArchiver archivedDataWithRootObject:array]; Of course all elements in your array needs to implement encodeWithCoder: . Bruce Lee Yuji's answer is right. but more accurately, your element of an array have to implement protocol and fillin your own code to methods

Storing NSObject using NSKeyedArchiver/ NSKeyedUnarchiver

匆匆过客 提交于 2019-12-04 22:25:37
I have a class object like; BookEntity.h import <Cocoa/Cocoa.h> @interface BookEntity : NSObject<NSCoding> { NSString *name; NSString *surname; NSString *email; } @property(copy) NSString *name,*surname,*email; @end BookEntity.m #import "BookEntity.h" @implementation BookEntity @synthesize name,surname,email; - (void) encodeWithCoder: (NSCoder *)coder { [coder encodeObject: self forKey:@"appEntity"]; } - (id) initWithCoder: (NSCoder *)coder { if (self = [super init]) { self = [coder decodeObjectForKey:@"appEntity"]; } return self; } and I assign some values to this class, BookEntity *entity =

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.