지난 주에 수 백만 번이 넘는 시간을 보냈습니다. (그리고 예, Apple의 문서를 읽었습니다.)보관 된 데이터를 복원 할 수 없습니다.
내 개체를 보관 중이며 파일 시스템에 기록 된 파일을 볼 수 있습니다. 파일 시스템을 검사하면 파일 내에서 내 데이터를 볼 수 있습니다.). 그러나 내 앱을 다시 실행하면 데이터가 복원되지 않습니다. 내가 읽은 모든 예는 이것이 얼마나 쉬운지를 알려주지 만, 나는 그것을 얻지 못하고있다. 하나의 유일한 점은 객체가 싱글 톤이라는 점입니다. 뷰 컨트롤러간에 데이터를 전달하는 데 사용됩니다.
정말 현자 조언에 감사드립니다. 미리 감사드립니다. 앱 위임에서
#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
저장 방법 :
- (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");
}
}
예제는 완벽했다 :
다음은 당신의 문제에 응답 할 수있는 포스트입니다. 감사! – Selch