5

I'm new to iOS programming and I've just learned some basics about saving/loading objects. In my book there is an example of saving an image to file:

NSData *data = UIImageJPEGRepresentation(someImage, 0.5);
[data writeToFile:imagePath atomically:YES];

My book also has an example of saving an "essay" object to file (an "essay" object has a string for title and another string for author):

essay.m conforms to the <NSCoding> protocol:

- (void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.essayTitle forKey:@"essayTitle"];
    [aCoder encodeObject:self.essayAuthor forKey:@"essayAuthor"];

}
- (instancetype) initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        _essayTitle = [aDecoder decodeObjectForKey:@"essayTitle"];
        _essayAuthor = [aDecoder decodeObjectForKey:@"essayAuthor"];
    }
    return self;
}

in essayStore.m:

[NSKeyedArchiver archiveRootObject:self.myEssay toFile:somePath];

I have three questions:

  1. When should I use NSData to save objects to a file/files and when should I conform to the <NSCoding> protocol to save objects to a file/files?

  2. When should I save all objects to a single file and when should I save one file for each object?

  3. If my essay object has an image in it, how can I save it with the image?

Thanks!

Demitri
  • 13,134
  • 4
  • 40
  • 41
wz366
  • 2,840
  • 6
  • 25
  • 34
  • The relationship is not as pronounced as you might think. NSCoding is a protocol, NSData is an opaque wrapper class. That is, any class that so wishes can be serialized by conforming to NSCoding and using the right kind of NSArchiver, but NSData represents more than just serialized classes. If you want to persist something to disk, you have more options than just NSCoding, though it does tend to be the simplest. – CodaFi Mar 27 '14 at 04:07
  • @CodaFi Can I use NSData to wrap any objects? – wz366 Mar 27 '14 at 04:56
  • 5
    NSCoding is used to convert complex data to primitive type based on some given keys, as you can not write non-primitive data objects to a file. Even an image is a complex data type. you need to convert it to primitive data type. – Anoop Vaidya Mar 27 '14 at 05:08
  • @AnoopVaidya "to convert it to primitive data type" do you mean an NSData object? – wz366 Mar 27 '14 at 05:21
  • @wz366: primitive type includes int, char, float, long, double etc. – Anoop Vaidya Mar 27 '14 at 05:29
  • @AnoopVaidya: you wrote "Even an image is a complex data type. you need to convert it to primitive data type." How do you convert an image to int, char, float, long or double? – wz366 Mar 27 '14 at 06:10
  • @wz366: image is complex `Cocoa Object` so you encode it using `encodeObject:forKey:` – Anoop Vaidya Mar 27 '14 at 07:09
  • To answer your question title : "What is the relationship between NSCoding and NSData?", NSData class conforms to that conforms to . you could check NSData header itself in Foundation framework. – boraseoksoon Jul 06 '16 at 07:10

1 Answers1

1

1. NSCoding is a protocol and is one of the methods of saving data to files. The advantage is that you can store your objects as such if using this method.
NSData, as mentioned in the Apple class reference is typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications.

2. This depends on your need to save data. Its usually enough to store all objects in a single file, and these are usually the objects that represent a entity.

3. You may refer to the following tutorial:
- List item NSCoding Tutorial for iOS

EDIT:

If you want to know about encoding images, take a look at this stackoverflow answer.
Although encoding images is not an option I would prefer. Complex objects need to be converted to primitive data as mentioned by @Anoop Vaidya in the comments.

And yes, it is possible to encode objects of classes created by you. But you have to implement NSCoding protocol in them. the following link is an apt example of a two level encoding, as you asked for:

Hope this helps!

Community
  • 1
  • 1
GoGreen
  • 2,251
  • 1
  • 17
  • 29
  • Thanks! I checked your tutorial and it didn't provide a way of saving an object that contains an image. It saves images separately just like what's on my book.[aCoder encodeObject:self.essayTitle forKey:@"essayTitle"] here self.essayTitle is a string object, can I encode objects that I created by me? – wz366 Mar 27 '14 at 04:32