I have an object that has an NSMutableArray of custom UIViews, a UIImage, and a couple of strings.
I take that object and shove it into a dictionary. The dictionary gets shoved into an NSData then I do this:
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [paths objectAtIndex:0];
NSString *fileName = DATA;
path = [path stringByAppendingPathComponent:fileName];
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:dict forKey: DATA];
[archiver finishEncoding];
[[NSFileManager defaultManager] createFileAtPath:path
contents:data
attributes:nil];
It saves successfully. When I try and load the data, the file system shows that my object was saved, but when I try and decode, it says my data is NULL.
Here is my decode code:
-(NSDictionary*)loadData
{
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
if (directoryContent != nil && [directoryContent count] > 0)
{
NSLog(@"CONTENT COUNT: %d",[directoryContent count]);
NSString *fileName = DATA;
path = [path stringByAppendingPathComponent:fileName];
NSData *data = [[NSMutableData alloc]initWithContentsOfFile:[directoryContent objectAtIndex:0]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *dict = [unarchiver decodeObjectForKey: DATA];
[unarchiver finishDecoding];
return dict;
}
else
return nil;
}
My app allows the user to move a bunch of views around and I am just trying to find a simple way to store the Views frames and transforms without having to extract every int/string value. I also was hoping to not have to break down my UIImage into a byte array and then reload it back into an UIImage.
Am I doing crazy stuff here, hoping for functionality that isn’t possible?
Is my only option to break down each UIView in the array into separate values for transforms, frames, etc?
Thoughts?
Does the document directory contain anything besides your data file? (I believe that it always contains at least a subdirectory named “Inbox”.) I noticed these lines in your
loadDatamethod:You append
fileNametopath, but then you don’t use it. You just read from the first file mentioned indirectoryContent.