I am probably not seeing something here, that is why I am asking for help 🙂
Here is the deal I have a NSMutable array of items that fulfill the NSCoding protocol, but NSKeyedArchiver always fails to archive it… here is my object implementation:
@implementation YTVideo
@synthesize URL,thumb,titulo;
#pragma mark NSCoding
#define kTituloKey @"titulo"
#define kURLKey @"URL"
#define kThumbKey @"thumb"
-(id)initWithData:(NSString *)ktitle :(UIImage *)kThumb :(NSURL *)kURL{
self.titulo = ktitle;
self.thumb = kThumb;
self.URL = kURL;
return self;
}
- (void) encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:titulo forKey:kTituloKey];
[encoder encodeObject:URL forKey:kURLKey];
NSData *thumbData = UIImagePNGRepresentation(thumb);
[encoder encodeObject:thumbData forKey:kThumbKey];
}
- (id)initWithCoder:(NSCoder *)decoder {
NSString* ktitulo = [decoder decodeObjectForKey:kTituloKey];
NSURL* kURL = [decoder decodeObjectForKey:kURLKey];
NSData* kThumbdata = [decoder decodeObjectForKey:kThumbKey];
UIImage* kThumb=[UIImage imageWithData:kThumbdata];
return [self initWithData:ktitulo:kThumb:kURL];
}
@end
During the program execution I have a NSMutable array of those objects called videosArray.
then, eventually, I try:
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:@"teste.wrapit"];
NSLog(@"PATH =%@",path);
bool teste = [NSKeyedArchiver archiveRootObject:videosArray toFile:path];
NSLog(@"aramzenamento:%@",teste ? @"sucesso!" :@"Nope");
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
NSLog(@"Arquivo armazenado existe?%@",fileExists ?@"Sim":@"Nao");
And I always get a fail on my boolean checks…
Any Ideas where I am completely wrong??
Thanks!!
The problem you’re experiencing has nothing to do with
NSKeyedArchiver. By the looks of it, you’re trying to archive your object at the root-level of your sandbox (the directory returned byNSHomeDirectory()). Try replacing the first line of the second block of code withAnother (perhaps cleaner) way to get the path of your
Documentsfolder is to use the C functionNSSearchPathForDirectoriesInDomains, which returns an array of paths that match the first argument:It’s worth pointing out that, on iOS, the function
NSSearchPathForDirectoriesInDomainis guaranteed to return anNSArraywith a single element when you use a built-in constant (likeNSDocumentDirectory) for the first argument, so you can safely useobjectAtIndex:0on the array.