Reputation: 566
Is it possible to save lots of UIBezierPaths? If so, how can it be done? I've tried putting them all into an NSMutableArray (and NSMutableDictionary) and then saving that array/dictionary to NSUserDefaults but I get this warning:
*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '(
"<UIBezierPath: 0x20b9ca10>",
"<UIBezierPath: 0x20a1dbf0>",
"<UIBezierPath: 0x20b9e550>",
)' of class '__NSArrayM'. Note that dictionaries and arrays in property lists must also contain only property values.
Upvotes: 1
Views: 2036
Reputation: 1070
Here's another solution that archives/unarchives the UIBezierPath directly:
- (NSURL *)documentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (void)archivePath:(UIBezierPath*)bPath withName:(NSString *)archiveName
{
NSString *savePath = [[[self documentsDirectory] URLByAppendingPathComponent:archiveName] path];
[NSKeyedArchiver archiveRootObject:bPath toFile:savePath];
}
- (UIBezierPath *)unarchivePathWithName:(NSString *)archiveName
{
return (UIBezierPath *)[NSKeyedUnarchiver unarchiveObjectWithFile:[[[self documentsDirectory] URLByAppendingPathComponent:archiveName] path]];
}
I'm not doing any error checking or verifying that the file exists, but this works if you're sure there's a file there. Here's an example of how to use it:
UIBezierPath *bez = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 500, 100)];
[bez appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)]];
[self archivePath:bez withName:@"path.archive"];
UIBezierPath *bez2 = [self unarchivePathWithName:@"path.archive"];
Upvotes: 0
Reputation: 4430
Try using this code
-(void)saveData :(NSMutableArray *)dataArray;
{
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];
[NSKeyedArchiver archiveRootObject:
dataArray toFile:dataFilePath];
}
-(NSMutableArray *)loadData;
{
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];
// Check if the file already exists
if ([filemgr fileExistsAtPath: dataFilePath])
{
NSMutableArray *dataArray;
dataArray = [NSKeyedUnarchiver
unarchiveObjectWithFile: dataFilePath];
return dataArray;
}
return NULL;
}
Should work fine, I tested this saving BezierPath and seems to work fine. I create a class called archiving which handles saving and loading arrays and dictionaries to and from the phone.
Upvotes: 4