NSKeyedUnarchiver with custom Objects - ios

I have a question about NSKeyedUnarchiver, NSMutableArray and self-created classes. The data is stored by NSKeyedArchiver to user defaults. When the data load into the user defaults will be printed to the console of the data stream. I want to unpack the data into an array, the array will be empty.
Maybe someone of you knows how I save the array or the array load.
Thank you.
Greetings,
Schumi

You should go through this documentation first. But basically, To enable archiving on custom class objects you will have to adopt the NSCoding protocol and implement initWithCoder: and encodeWithCoder: methods.

Related

NSCoding to CKRecord

I have a custom class with which i made an (swift-y) struct NSCodable. Now I want to convert that into a CKRecord in order to use CloudKit. Even though I set 'key value'-pairs when encoding my struct, it is in my understanding that the struct is converted into NSData and that you can't convert it to a Dictionary (or another key-value object). So I get the feeling that this is not the way to go.
Is there a way to make this conversion directly? Or with a step in-between (for instance converting the Data into a [String: String]- dictionary)?
NSCoding is only for going from/to the same class. Instead, write a toServerDictionary method on your custom class, including only the properties you want to send to CloudKit, and then use the result to call setValuesForKeys on a CKRecord.
You'll likely find there are properties that need to be specific types, in which case its better to make your method toServerRecord and create a CKRecord and return it. You can also have a updateWithServerRecord to set anything you receive back.

Saving custom object to NSUserDefaults - can't set encoder and decoder methods

How would you be able to save a custom object to NSUserDefaults??
NSUserDefaults.standardUserDefaults().setObject(obj, forKey: "object")
NSUserDefaults.standardUserDefaults().synchronize()
The object is of the SPTAuthViewController class in the Spotify iOS SDK, so I can't edit it to add encoder and decoder methods. How can I encode the object into an NSData object to store it in NSUserDefaults?
You can't store custom objects into user defaults. You can only store "property list objects" (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects).
In order to save a custom object to user defaults you have to convert it to one of those types.
You can make your custom object conform to the NSCoding protocol. To do that you have to implement the init(coder:) and encode(coder:) methods. (I believe that's the swift form of the method names - I've been working in Objective-C lately so my Swift is getting rusty.) How you implement those methods depends on your class. Typically you make multiple calls to encodeObject:forKey: to encode your object's properties.
Once your custom object conforms to NSCoding you can convert it to NSData using the NSKeyedArchiver method archivedDataWithRootObject(). You then convert it from NSData back to the original object using the NSKeyedUnarchiver method unarchiveObjectWithData().
EDIT:
I just realized that you were trying to save a view controller object (SPTAuthViewController to user defaults. You should not serialize and save view controller objects. Controller objects should not be used to save application data.

Can you save Parse information in NSUserDefaults? (IOS)

Basically, i want to save my current Parse user's friends into NSUserDefaults. For some reason it doesn't seem to be saving correctly or it won't let me save them. Is this because the array of objects(friends) that i am returning is to intricate for NSUserDefaults to handle?
I have tried turning the array of objects into NSData and saving it that way but i still haven't yielded any good results.
I would really appreciate if someone could help me out.
Cheers.
You really should not use NSUserDefaults for storing this type of information. NSUserDefaults was created in order to store and retrieve "settings" type information as opposed to data.
As for your question, in order to save custom objects you need to implement these 2 methods:
- (void)encodeWithCoder:(NSCoder *)encoder {
}
- (id)initWithCoder:(NSCoder *)decoder {
}
These methods will essentially tell the application how to store the objects data in a file as well as how to read it back in and create an object from it.
Documentation:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Archiving/Articles/codingobjects.html
However, if you are looking to store these objects locally you should look into Parse's local caching feature. You could also use Core Data to store the objects locally but maintaining 2 instances of the objects may be ugly.
Parse Local Caching:
https://www.parse.com/docs/ios_guide#localdatastore/iOS

How to save NSMutableArray to Core Data?

I know it's not recommended to save NSMutableArray to Core Data but I need to access the array on another view controller (using one of the segue methods (such as passing a variable to another view controller) won't do the job).
So unless there's another way that I'm not aware of, how can I save the array to core data?
I'm using Swift.
I think your question is not how to save NSMutableArray to Core Data, instead, your question seems like is How to access data on another viewController?
First, I think you can get some knowledge about Delegate.
Second,Here is a Q&A I can share with you which I think may help you to figure out your confused.Passing Data between View Controllers
Saving NSArray to Core Data is possible via Transformable-type attributes.
Saving NSMutableArray is not possible. Only immutable ones.
if you want to save a mutable array to core data, use copy method. It will return immutable copy of your mutable object.

iOS CoreData - Serialize entire object instead of creating Entity

I am working on an email client and I want to persist emails into CoreData.
Looking at some tutorials, it seems I need to create a custom class (entity) and add attributes for each field I want to persist.
The problem is that I have an array of pretty complex objects (http://libmailcore.com/mailcore2/api/Classes/MCOIMAPMessage.html) I want to serialize and persist. Is there an easier way to do this?
Try converting the MCOIMAPMessage to NSData and then you can persist it to coredata. Check this SO post on how to convert NSObject to NSData. Hope this helps.

Resources