Database Reference
In-Depth Information
else
_data = [[CTData alloc] init];
}
return _data;
}
-(CTMetadata *)metadata {
if(_metadata == nil){
if(_fileWrapper != nil)
_metadata = [self decodeObjectFromWrapperWithKey:kMetadataKey];
else
_metadata = [[CTMetadata alloc] init];
}
return _metadata;
}
Both of these methods are identical except that we are working with
CTData
in one and
CTMetadata
in
the other. With that being said, let's just walk through the first one for CTD
a
ta.
When
[self data]
or
self.data
is called, this method will be called. It first checks to see whether
_data
is equal to
nil
. If it isn't, then we just pass back our
_data
object. If it is nil, then we check to see
if our
NSFileWrapper
property has been set. If it has, we use it to call
decodeObjectFromWrapperWithKey
:
passing in our
constant kDataKey
. If it hasn't, we initialize a blank
CTData
object.
We can think of the process like this. When we first access a saved document, we have a file
wrapper. Therefore, we can decode it with that filewrapper, which in turn gives us our instantiated
CTData
object. However, when we create a brand new document we have decoded anything so we
will create blank
CTData
and
CTMetadata
objects instead.
Now we need to add all our public access methods that we added in our interface.
-(NSString *)firstName {
return [self.data firstName];
}
-(void)setFirstName:(NSString *)firstName {
if([[self.data firstName] isEqualToString:firstName])
return;
NSString *oldFirstName = [self.data firstName];
[self.data setFirstName:firstName];
[self.undoManager setActionName:@"First Name Change"];
[self.undoManager registerUndoWithTarget:self selector:@selector(setFirstName:)
object:oldFirstName];
}
-(NSString *)lastName {
return [self.data lastName];
}
Search WWH ::
Custom Search