iOS 5アプリでアーカイブを動作させる方法を理解できません。初期化時に存在する場合、plistデータを取得するシングルトンセッションストアがあります。 SessionStoreはNSObjectから継承し、plistファイルからロードしたいNSMutableArray * allSessionsという1つのivarを持っています。ここにSessionStore.mがあります。問題が明らかか、もっと情報が必要なのかどうかわかりません...ありがとう! AppDelegate.mでネイサンシングルトンでinitWithCoderとNSUnarchiverを実装するのに手助けが必要
#import "SessionStore.h"
static SessionStore *defaultStore = nil;
@implementation SessionStore
+(SessionStore *)defaultStore {
if (!defaultStore) {
// Load data.plist if it exists
NSString *pathInDocuments = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"data.plist"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:pathInDocuments])
defaultStore = [NSKeyedUnarchiver unarchiveObjectWithFile:pathInDocuments];
} else
defaultStore = [[super allocWithZone:NULL] init];
return defaultStore;
}
+(id)allocWithZone:(NSZone *)zone {
return [self defaultStore];
}
-(id)init {
if (defaultStore)
return defaultStore;
self = [super init];
if (self)
allSessions = [[NSMutableArray alloc] init];
return self;
}
-(NSMutableArray *)allSessions {
if (!allSessions) allSessions = [[NSMutableArray alloc] init];
return allSessions;
}
-(void)setAllSessions:(NSMutableArray *)sessions {
allSessions = sessions;
}
-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:allSessions forKey:@"All Sessions"];
}
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [SessionStore defaultStore];
[self setAllSessions:[aDecoder decodeObjectForKey:@"All Sessions"]];
return self;
}
それが終了時にplistファイル保存します。私は通常これを行う方法は、私は、私がする必要があるときに物事を保存し、データファイルを持つことです
- (void)applicationWillTerminate:(UIApplication *)application
{
// Save data to plist file
NSString *pathInDocuments = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"data.plist"];
[NSKeyedArchiver archiveRootObject:[SessionStore defaultStore] toFile:pathInDocuments];
}
どうしたのですか? –
plistファイルを読み込もうとするとフリーズします。 "EXC_BAD_ACCESS"行:NSString * pathInDocuments = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory、NSUserDomainMask、YES)lastObject] stringByAppendingPathComponent:@ "data.plist"]; –