2013-05-30 90 views
3

所以我想通過應用程序沙箱文檔目錄存檔我的對象。我複製了Big Nerd Ranch的iOS編程的大部分代碼,所以我沒有看到可能出錯的地方。無論如何,我可以診斷進程的哪一部分不起作用,因爲它們中沒有一個提出任何錯誤,但仍然無法保存。這是我的對象代碼。NSCoding的iOS存檔對象失敗

@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

這可能會發生一些時間:) –