好吧,我在上周已经超过了这一百万次而且我没有得到它。 (是的,我读过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");
}
}
答案 0 :(得分: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.
}
如果您保存文件时可以将其解压缩,但无法重启应用程序,则文件出现问题。所有这些信息都应指向解决方案。
另一个想法 - 当你对数据进行编码时,记录它,只是为了确保它不是零 - 但即使如此,unarchive也应该有效。
答案 1 :(得分:0)
在sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:[SharedAppDataObject archivePath]];
中初始化application:didFinishLaunchingWithOptions:
。此方法用于初始化数据结构并恢复以前的应用程序状态。
另外,从static SharedAppDataObject *sharedStore = nil;
取出sharedStore
。如果保存文件存在,[ShareAppDataObject sharedStore]
将始终取消归档不必要的文件。它可以在初始化期间取消存档一次。
以下是可以回答您问题的帖子:http://bit.ly/PJO8fM