ICloud - How to save an archive containing an array of custom objects

I developed a small application that is stored locally in iOS by archiving an array of user objects containing:

@property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *dateCreated; @property (nonatomic, copy) NSString *desc; @property (nonatomic, copy) NSString *url; 

I want to synchronize the specified archive using iCloud, and I believe that the recommended mechanism is through a subclass of UIDocument. All the UIDocument examples that I found used a single instance with one single NSString, so I got a little confused how to synchronize the entire array of user objects, but using UIDocument (for example, I do it locally through NSCoding).

Should I create an array of UIDocument objects containing the above properties, should I create an instance of UIDocument containing 1 instance of the data object described above, and then create an array containing all instances, or should it contain one single UIDocument full array of user objects?

I did some research, but I'm still confused. As a result, I will need to synchronize only one file containing an array of the specified user objects.

Thank you in advance for your help.

Today I have a custom class, as described above, with 4 lines called Snippet, and in my Root view controller. I have a list of NSMutableArray names in which I add every new instance of this Snippet class.

 self.list = [[NSMutableArray alloc] init]; Snippet *newEntry = [[Snippet alloc] init]; [self.list addObject:newEntry]; 

Should I create a subclass of UI Document that owns an array of custom objects?

+4
source share
1 answer

The example in the docs really shows a subclass of UIDocument, which has only one row, but returns NSData from -contentsForType:error: You can store as many objects as you want in NSData using NSKeyedArchiver. Read Serializing Objects to learn how to encode objects using NSKeyedArchiver (and keep reading to find out how to return them!).

Using your properties as an example ...

 @interface MyDocument : UIDocument { } @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *dateCreated; @property (nonatomic, copy) NSString *desc; @property (nonatomic, copy) NSString *url; @end @implementation MyDocument //... - (id)contentsForType:(NSString *)typeName error:(NSError **)outError { NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:name forKey:@"name"]; [archiver encodeObject:dateCreated forKey:@"created"]; [archiver encodeObject:desc forKey:@"desc"]; [archiver encodeObject:url forKey:@"url"]; [archiver finishEncoding]; // release archiver if you're not using ARC return data; } @end; 

WARNING: I did not compile the code above, so no guarantees. This should serve as an example to illustrate the use of the archiver to store several objects in one data object, which you can return as the contents of your document.

+2
source

Source: https://habr.com/ru/post/1385955/


All Articles