1

好的,上周我已经超过了一百万次,但我就是不明白。(是的,我读过 Apple 的文档。)

我正在归档我的对象,它似乎正在正确归档(我可以看到写入文件系统的文件,如果我检查它,我可以在其中看到我的数据)。但是,当我重新启动我的应用程序时,我的数据没有被恢复。我读到的每个例子都告诉我这是多么容易,但我就是不明白。一个独特的事情是我的对象是一个单例,它用于在视图控制器之间传递数据。

我真的很感激一些明智的建议。提前致谢。

这是我的标题:

#import <Foundation/Foundation.h>

@interface SharedAppDataObject : NSObject <NSCoding>
{
    NSMutableDictionary *apiKeyDictionary;
    NSString *skuFieldText;
    NSIndexPath *checkmarkIndex;
}

+ (SharedAppDataObject *)sharedStore;

@property (nonatomic, copy) NSString *skuFieldText;
@property (nonatomic, copy) NSIndexPath *checkmarkIndex;
@property (nonatomic, copy) NSMutableDictionary *apiKeyDictionary;

-(void)setValue:(NSString *)apiKey forKey:(NSString *)name;

-(void)setSkuField:(NSString *)s;
-(void)setCheckmarkIndex:(NSIndexPath *)p;

-(NSMutableDictionary *)apiKeyDictionary;
-(BOOL)saveChanges;

@end

这是我的实现:

#import "SharedAppDataObject.h"

@implementation SharedAppDataObject

@synthesize skuFieldText;
@synthesize checkmarkIndex;
@synthesize apiKeyDictionary;


    //create our shared singleton store
+(SharedAppDataObject *)sharedStore {

    static SharedAppDataObject *sharedStore = nil;

    if (!sharedStore) {
        sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]];

        if(!sharedStore) 
            sharedStore = [[super allocWithZone:NULL] init];
    }
    return sharedStore;
}

-(id) init {
    self = [super init];
    if (self) {
    } 
    return self;
}

-(void)setValue:(id)apiKey forKey:(NSString *)name {
        [apiKeyDictionary setObject:apiKey forKey:name];
}

-(void)setSkuField:(NSString *)s {
    skuFieldText = s;
}

-(NSMutableDictionary *)apiKeyDictionary {  
    return apiKeyDictionary;
}

-(void)setCheckmarkIndex:(NSIndexPath *)p {
    checkmarkIndex = p;   
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:skuFieldText forKey:@"skuFieldText"];
    [aCoder encodeObject:checkmarkIndex forKey:@"checkmarkIndex"];
    [aCoder encodeObject:apiKeyDictionary forKey:@"apiKeyDictionary"];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        [self setSkuFieldText:[aDecoder decodeObjectForKey:@"skuFieldText"]];
        [self setCheckmarkIndex:[aDecoder decodeObjectForKey:@"checkmarkIndex"]];
        [self setApiKeyDictionary:[aDecoder decodeObjectForKey:@"apiKeyDictionary"]];
    }
    return self;
}

+(NSString *)archivePath {
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [documentDirectories objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"bbyo.archive"];
}

-(BOOL)saveChanges {      
    return [NSKeyedArchiver archiveRootObject:self toFile:[SharedAppDataObject archivePath]];  
}

@end

来自 App Delegate 的保存方法:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    BOOL success = [[SharedAppDataObject sharedStore] saveChanges];

    if (success) {
        NSLog(@"Saved all the data");
    } else {
        NSLog(@"Didn't save any of the data");
    }
}
4

2 回答 2

0

我不能给你答案,但有一些想法可以解决这个问题。采取这条线:

sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]];

因此,如果 sharedStore 为 nil,则说明有问题 - 请对其进行测试。如果什么都没有,那么记录路径,并使用 NSFileManager 方法查看文件是否存在,它的大小等。如果你发现文件在那里并且有大小,但你不能取消归档它,那当然是一个问题。在这种情况下,请在创建文件后添加特殊调试代码:

-(BOOL)saveChanges {      
    BOO ret =  [NSKeyedArchiver archiveRootObject:self toFile:[SharedAppDataObject archivePath]];
    id foo = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]];
    // check if foo is not nil, if its the proper class, etc.
}

如果您在保存文件时可以很好地取消存档,但在重新启动应用程序时无法解压,则说明文件有问题。所有这些信息都应该指向解决方案。

另一个想法——当你对数据进行编码时,记录它,以确保它不是零——但即使是这样,解档也应该起作用。

于 2012-09-20T19:27:56.830 回答
0

sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]];中初始化application:didFinishLaunchingWithOptions:。此方法用于初始化数据结构并恢复以前的应用程序状态。

另外,static SharedAppDataObject *sharedStore = nil;sharedStore. 如果保存文件存在,[ShareAppDataObject sharedStore]将始终取消归档不必要的文件。它可以在初始化期间取消归档一次。

这是一个可以回答您问题的帖子:http: //bit.ly/PJO8fM

于 2012-09-21T01:49:41.060 回答