1
기본적으로 클래스 클래스 인스턴스가 있습니다.이 클래스 인스턴스는 기본적으로 다른 클래스의 개체에서 변경할 수있는 배열입니다. 나는 이것이 영구적 인 데이터를 저장해야하지만 문제가 있어요, 내보기 컨트롤러의 방법을 클래스 데이터 유형의 지속 데이터
-(void)archiveFavorites {
Singleton *singleton = [Singleton sharedSingleton];
NSLog(@"archiving");
GameDatabase *favoritesToArchive = singleton.favorites;
[NSKeyedArchiver archiveRootObject:favoritesToArchive toFile:[self archivePath]];
}
-(NSString *)archivePath {
NSString *docDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
return [docDir stringByAppendingPathComponent: @"Favorites.dat"];
}
를 사용하여 아카이브를 시도했다. 그때
기본적으로 내가 아카이브에 내 자신의 클래스 (GameDatabase)의 인스턴스를 저장하기 위해 노력하고있어 "배경을 입력 않은 응용 프로그램의 위치"내 응용 프로그램 위임에
[viewController archiveFavorites];
를 호출합니다. 나는 예외를 발생하고 때 아카이브 :
편집 : 좋아, 그래서를 heres 내가 내 두 클래스에서 현재 구현하기 위해 노력하고 있습니다 : GameDatabase.m
- (void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:games forKey:@"Games"];
}
- (id)initWithCoder:(NSCoder *)decoder{
if(self = [super init])
{
games = [decoder decodeObjectForKey:@"Games"];
}
return self;
}
GameRecord.m
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.name = [decoder decodeObjectForKey:@"Name"];
self.gameRules = [decoder decodeObjectForKey:@"GameRules"];
self.players = [decoder decodeIntForKey:@"Players"];
self.gameType = [decoder decodeIntForKey:@"GameType"];
self.drinkType = [decoder decodeIntForKey:@"DrinkType"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.name forKey:@"Name"];
[encoder encodeObject:self.gameRules forKey:@"GameRules"];
[encoder encodeInt:self.players forKey:@"Players"];
[encoder encodeInt:self.gameType forKey:@"GameType"];
[encoder encodeInt:self.drinkType forKey:@"DrinkType"];
}
프로토콜에 추가하거나 두 클래스 만 구현해야합니까? –
두 가지 방법이 프로토콜입니다. 다른 질문을하지 않는 한? –
와우 내가했던 모든 두 가지 방법을 추가하고 작동하는 것 같았지만, 그들에 어떤 코드를 넣지 않았어. 그 맞습니까? –