views:

23

answers:

1

Hello guys... 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!!

+2  A: 

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 by NSHomeDirectory()). Try replacing the first line of the second block of code with

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
path = [path stringByAppendingPathComponent:@"teste.wrapit"];

Another (perhaps cleaner) way to get the path of your Documents folder is to use the C function NSSearchPathForDirectoriesInDomains, which returns an array of paths that match the first argument:

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"teste.wrapit"];

It's worth pointing out that, on iOS, the function NSSearchPathForDirectoriesInDomain is guaranteed to return an NSArray with a single element when you use a built-in constant (like NSDocumentDirectory) for the first argument, so you can safely use objectAtIndex:0 on the array.

L Cassarani
Dude... Thanks a LOT!!!
Marsson
No problem :) If my answer fixed the problem you were having, perhaps you would like to accept it by clicking on the check box mark to the left. Thanks!
L Cassarani
Did it... I just had to wait 2 mins to accept the answer as right, was waiting for the cooldown...
Marsson