Saving Documents Document Based Apps - moralesalberto-cocoa/drawing GitHub Wiki
In cocoa use the template for document based app, and that will get you a long way. Then you have to implement the save and read methods.
A good tutorial on this is at:
http://www.youtube.com/watch?v=yfiOGQmUVjE
https://github.com/lucasd2/AppleProg-Cocoa-Tutorials/tree/master/Lesson%2022
http://appleprog.com/tutorial-list/
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
@throw exception;
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
@throw exception;
return YES;
}
The basic changes to the above methods turned to the code below. I simply archived the NSMutableArray archivedShapes
.
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data
if(outError) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
self.archivedShapes = [NSMutableArray arrayWithArray: self.drawingView.shapes];
return [NSKeyedArchiver archivedDataWithRootObject:self.archivedShapes];
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type.
if(outError) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
[self setArchivedShapes:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
return YES;
}
Then each object in that array had to have two methods defined. In my case the object was Shape.
// Encoding and reading
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
NSPoint startP = [aDecoder decodePointForKey:@"startPoint"];
NSPoint endP = [aDecoder decodePointForKey:@"endPoint"];
[self resetWithStartPoint:startP andEndPoint:endP];
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
[aCoder encodePoint:self.startPoint forKey:@"startPoint"];
[aCoder encodePoint:self.endPoint forKey:@"endPoint"];
}