nskeyedarchiver

How to archive an NSArray of custom objects to file in Objective-C

ⅰ亾dé卋堺 提交于 2019-12-19 09:16:32
问题 Can you show me the syntax or any sample programs to archive an NSArray of custom objects in Objective-C? 回答1: Check out NSUserDefaults . For Archiving your array, you can use the following code: [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@"mySavedArray"]; And then for loading the custom objects in the array you can use this code: NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults]; NSData *savedArray =

Need to archive CLLocation data

丶灬走出姿态 提交于 2019-12-19 07:56:13
问题 I have an array of CLLocation data that I would like to archive. Should the NSUserDefaults system be used? Otherwise, how best to archive the CLLocation data? 回答1: UserDefaults can only store certain types of data, and should be used to store user preference information, not arbitrary application state data. To quote the Apple docs: The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its

Encode NSArray or NSDictionary using NSCoder

喜你入骨 提交于 2019-12-17 18:37:49
问题 I was wondering whether or not it is possible to use the NSCoder method: - (void)encodeObject:(id)objv forKey:(NSString *)key to encode either an instance of NSArray or NSDictionary. If not how do you go about encoding them? I am trying to use NSKeyedArchived / NSKeyedUnarchiver to send data between phones using GameKit. I tried it and cannot seem to retrieve the string I stored in my array. The packet class I made comes through along with the packet type integer, but not the data in the

Save and retrieve value via KeyChain

做~自己de王妃 提交于 2019-12-17 10:25:25
问题 I'm trying to store an Integer and retrieve it using KeyChain. This is how I save it: func SaveNumberOfImagesTaken() { let key = "IMAGE_TAKEN" var taken = 10 let data = NSKeyedArchiver.archivedDataWithRootObject(taken) let query : [String:AnyObject] = [ kSecClass as String : kSecClassGenericPassword, kSecAttrAccount as String : key, kSecValueData as String : data ] let status : OSStatus = SecItemAdd(query as CFDictionaryRef, nil) } This is how I try to retrieve it: func

Save and retrieve value via KeyChain

删除回忆录丶 提交于 2019-12-17 10:25:18
问题 I'm trying to store an Integer and retrieve it using KeyChain. This is how I save it: func SaveNumberOfImagesTaken() { let key = "IMAGE_TAKEN" var taken = 10 let data = NSKeyedArchiver.archivedDataWithRootObject(taken) let query : [String:AnyObject] = [ kSecClass as String : kSecClassGenericPassword, kSecAttrAccount as String : key, kSecValueData as String : data ] let status : OSStatus = SecItemAdd(query as CFDictionaryRef, nil) } This is how I try to retrieve it: func

Valid file path for archiverootobject and unarchiverootobject

℡╲_俬逩灬. 提交于 2019-12-17 07:56:08
问题 I am making an iPhone app and in a previous question I was told that I needed a valid file path for archiverootobject and unarchiverootobject but I don't know how to make one. So what is a valid file path to save arrays of custom classes? Follow up question: On the linked question I have set up my methods to use invalid saves but it still works would anyone know why? 回答1: You can save it to a subdirectory folder named with your bundleID inside the application support folder or you can also

How do I archive two objects using NSKeyedArchiever?

大兔子大兔子 提交于 2019-12-13 02:13:33
问题 Previously, I have an array in which I use NSKeyedArchiver to archieve. [NSKeyedArchiver archiveRootObject:array toFile:docPath]; Called on applicationWillTerminate and applicationDidEnterBackground . Upon starting up, in didFinishLaunchingWithOPtions , the array is unarchieved: NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath]; Everything was good. Since then, I added a Singleton class for Settings variables. I would like to archive the Singleton as well. How would

Archiving and Unarchiving results in Bad Access

耗尽温柔 提交于 2019-12-12 16:30:50
问题 I'm having trouble setting up a model object to save the visual state of user generated CALayers in a simple graphics application for the iphone. I'm attempting to save the background color and frame of all the current layers on screen by passing those properties to model objects which implement the NSCoding protocol and then into an NSMutableArray which the app delegate owns. Then I archive the array with NSKeyedArchiver and store it in the NSUserDefaults. Each CALayer's backgroundColor

Loading a Singleton's state from NSKeyedArchiver

我的梦境 提交于 2019-12-12 07:59:03
问题 I have a class that I've made into a singleton and am able to save it's state using an NSKeyedArchiver, but I can't wrap my head around pulling it's state back out. In the function that does the loading I have Venue *venue = [Venue sharedVenue]; NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; venue = [unarchiver decodeObjectForKey:@"Venue"]; [unarchiver finishDecoding];

Objective-C best choice for saving data

瘦欲@ 提交于 2019-12-12 04:31:08
问题 I'm currently looking for the best way to save data in my iPhone application; data that will persist between opening and closing of the application. I've looked into archiving using a NSKeyedArchiver and I have been successful in making it work. However, I've noticed that if I try to save multiple objects, they keep getting overwritten every time I save. (Essentially, the user will be able to create a list of things he/she wants, save the list, create a few more lists, save them all, then be