2013-05-30 3 views
3

Итак, я хочу, чтобы архивировать мой объект в каталог документов песочницы приложений. Я скопировал большую часть кода из iOS-программирования Big Nerd Ranch, поэтому я не вижу, что может пойти не так. Есть ли в любом случае я мог бы диагностировать, какая часть процесса не работает, поскольку не из них возникает какая-либо ошибка, но все еще не удалось сохранить. Вот мой объектный код.Объект архивации iOS с ошибкой NSCoding

@interface Post : NSObject <NSCoding> 
{ 
    NSString *title; 
    NSString *date; 
    NSString *content; 
    NSString *type; 
    UIImage *image; 
    NSString *timestamp; 
} 

@property (nonatomic) NSString *title; 
@property (nonatomic) NSString *date; 
@property (nonatomic) NSString *content; 
@property (nonatomic) NSString *type; 
@property (nonatomic) UIImage *image; 
@property (nonatomic) NSString *timestamp; 

@end 


@implementation Post 
@synthesize title; 
@synthesize date; 
@synthesize content; 
@synthesize type; 
@synthesize image; 
@synthesize timestamp; 
#pragma mark - NSCoding Protocol 
- (void)encodeWithCoder:(NSCoder *)aCoder 
{ 
    [aCoder encodeObject:title forKey:@"title"]; 
    [aCoder encodeObject:date forKey:@"date"]; 
    [aCoder encodeObject:content forKey:@"content"]; 
    [aCoder encodeObject:type forKey:@"type"]; 
    [aCoder encodeObject:image forKey:@"image"]; 
    [aCoder encodeObject:timestamp forKey:@"timestamp"]; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super init]; 
    if (self){ 
     [self setTitle:[aDecoder decodeObjectForKey:@"title"]]; 
     [self setDate:[aDecoder decodeObjectForKey:@"date"]]; 
     [self setContent:[aDecoder decodeObjectForKey:@"content"]]; 
     [self setType:[aDecoder decodeObjectForKey:@"type"]]; 
     [self setImage:[aDecoder decodeObjectForKey:@"image"]]; 
     [self setTimestamp:[aDecoder decodeObjectForKey:@"timestamp"]]; 
    } 
    return self; 
} 

я сохранить архив его с помощью магазина с методами последующей

- (NSString *)itemArchivePath 
{ 
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 

    //Get one and only document directory form that list 
    NSString *documentDirectory = [documentDirectories objectAtIndex:0]; 
    return [documentDirectory stringByAppendingPathComponent:@"posts.archive"]; 
} 

- (BOOL)saveChanges 
{ 
    //return success of failure 
    NSString *path = [self itemArchivePath]; 
    NSLog(@"save path = %@",path); 
    return [NSKeyedArchiver archiveRootObject:allPosts toFile:path]; 
} 

я назвал метод сохранения в applicationDidEnterBackground сохранить.

- (void)applicationDidEnterBackground:(UIApplication *)application 
{  
    BOOL success = [[PostStore sharedStore] saveChanges]; 
    if (success) { 
     NSLog(@"Saved all Posts"); 
    } else { 
     NSLog(@"Could not save any Posts"); 
    } 
} 
+0

Что вы имеете в виду под "не удалось спасти"? Файл не создается? Файл не может быть прочитан снова? IPad распадается, открывая ворота в подземный мир? – borrrden

+1

Однако одна вещь, которую я замечаю, заключается в том, что вы использовали NSDocumentationDirectory вместо NSDocumentDirectory. – borrrden

ответ

2

Вы экономите до NSDocumentationDirectory вместо NSDocumentDirectory

Заменить

NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 

С

NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
+0

Большое спасибо, какая глупая ошибка. – harinsa

+0

Это может произойти некоторое время :) –

Смежные вопросы