Database Reference
In-Depth Information
include a file type version with your
UIDocuments
so that down the line you have flexibility if you need
to change your document model in future releases.
The next thing we do is convert our thumbnail data into an
NSData
object using
UIImagePNGRepresentation()
. Next we call
encodeObject:forKey:
twice on our
NSCoder
object. The
first one is for our thumbnail data and the second is for our display name string.
Moving on to the
initWithCoder:
method, we also receive an
NSCoder
object as the parameter.
Because this is our read method, we start out by calling the
decodeIntForKey:
method on our
NSCoder
with the key
kVersionKey
. Now that we have extracted the version number, we check to
see if it is equal to 1. If it isn't, we just return a blank initialized
CTMetadata
object. If it is, we use the
decodeObjectForKey
: method to get our Image data and our display name. The image data then has
to be converted to a
UIImage
object using the
imageWithData:
class method on
UIImage
. Finally we
return our fully initialized
CTMetadata
object by calling
initWithThumbnail:andDisplayName:
.
That is all there is to our
CTMetadata
object. Now we need to create our actual data model. Let's
create another new Objective-C class that is a subclass of
NSObject
and call it
CTData
. Just like we
did with
CTMetadata
we want to subscribe to the
NSCoding
protocol. Then we will add some public
properties.
@property (strong) NSString *firstName;
@property (strong) NSString *lastName;
@property (strong) NSString *displayName;
@property (strong) NSNumber *favoriteNumber;
@property (strong) UIImage *photo;
Now we can move on over to the
CTData.m
file. We will be doing the exact same thing that we did in
the
CTMetadata.m
file. Here is what your
.m
file should look like when you are done:
#import "CTData.h"
#define kVersionKey @"VERSION"
#define kFirstNameKey @"FIRST_NAME"
#define kLastNameKey @"LAST_NAME"
#define kDisplayNameKey @"DISPLAY_NAME"
#define kFavoriteNumberKey @"FAVORITE_NUMBER"
#define kPhotoKey @"PHOTO"
@implementation CTData
- (id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName displayName:(NSString *)
displayName favoriteNumber:(NSNumber *)favoriteNumber andPhoto:(UIImage *)photo {
if((self = [super init])){
_firstName = firstName;
_lastName = lastName;
_displayName = displayName;
_favoriteNumber = favoriteNumber;
_photo = photo;
}
return self;
}
Search WWH ::
Custom Search