필자는 플래시 카드 응용 프로그램을위한 다중 레벨 데이터를 저장해야하며, 1) 데이터를 관리하는 방법과 2) 저장 방법을 알아내는 데 도움이 될 수 있습니다.다중 레벨 데이터를 저장하기위한 클래스를 만드는 방법은 무엇입니까?
이 데이터는 다음과 같이 분류됩니다 : A) 카드 2 문자열 B) 팩은 문자열을 포함하는 "PackName"및 카드 C의 배열) 데크는 문자열 "DeckName"을 포함하고 팩
의 배열을 포함지금은 카드, 팩, 갑판의 3 가지 수업이 있습니다.
//Card.h
@interface Card : NSObject {
NSString *primaryPhrase;
NSString *secondaryPhrase;
}
@property (nonatomic,retain)NSString *primaryPhrase;
@property (nonatomic,retain)NSString *secondaryPhrase;
@end
Card.m
@implementation Card
@synthesize primaryPhrase;
@synthesize secondaryPhrase;
-(id)init{
if(self=[super init]){
}
return self;
}
@end
Pack.h
@interface Pack : NSObject{
NSString *packName;
NSMutableArray *cards; //array of card classes
BOOL isInUse;
}
@property (nonatomic,retain)NSMutableArray *cards;
@property (nonatomic,retain)NSString *packName;
@property (nonatomic,assign)BOOL isInUse;
@end
Pack.m
@implementation Pack
@synthesize packName;
@synthesize cards;
@synthesize isInUse;
-(id)init{
if(self=[super init]){
self.isInUse=YES;
}
return self;
}
@end
Deck.h
@interface Deck : NSObject <NSCoding>{
NSString *deckName;
NSMutableArray *packs; //array of pack classes
NSString *primaryLang;
NSString *secondaryLang;
}
@property (nonatomic,retain)NSMutableArray *packs;
@property (nonatomic,retain)NSString *deckName;
@property (nonatomic,retain)NSString *primaryLang;
@property (nonatomic,retain)NSString *secondaryLang;
- (void) encodeWithCoder:(NSCoder*)encoder;
- (id) initWithCoder:(NSCoder*)decoder;
@end
Deck.m
#import "Deck.h"
@implementation Deck
@synthesize packs;
@synthesize deckName;
@synthesize primaryLang;
@synthesize secondaryLang;
//Default settings for each new Deck
-(id)init{
if(self=[super init]){
}
return self;
}
-(void)encodeWithCoder:(NSCoder*)encoder{
[encoder encodeObject:packs forKey:@"packs"];
[encoder encodeObject:deckName forKey:@"deckName"];
[encoder encodeObject:primaryLang forKey:@"primaryLang"];
[encoder encodeObject:secondaryLang forKey:@"secondaryLang"];
}
-(id)initWithCoder:(NSCoder*)decoder{
if(self=[super init]){
packs=[decoder decodeObjectForKey:@"packs"];
deckName=[decoder decodeObjectForKey:@"deckName"];
primaryLang=[decoder decodeObjectForKey:@"primaryLang"];
secondaryLang=[decoder decodeObjectForKey:@"secondaryLang"];
}
return self;
}
@end
은 그 때 나는 다시 카드를 포함 덱을 보유하는있는 NSMutableArray "allDecks"를 사용하지만, 난이 (오류를 작동하지 얻을 수 없었던, 그러나 "팩 이름이"항상 null입니다) :
for(int i=0; i<=2; i++){
Deck *newdeck=[[Deck alloc]init];
[globDat.allDecks addObject:newdeck];
}
((Deck *)[globDat.allDecks objectAtIndex:0])[email protected]"DeckName 0";
((Deck *)[globDat.allDecks objectAtIndex:1])[email protected]"DeckName 1";
((Deck *)[globDat.allDecks objectAtIndex:2])[email protected]"DeckName 2";
for(int i=0; i<=2; i++){
Pack *newpack=[[Pack alloc] init];
[((Deck *)[globDat.allDecks objectAtIndex:i]).packs addObject:newpack];
}
for(int j=0; j<+2; j++){
((Pack *)[((Deck *)[globDat.allDecks objectAtIndex:0]).packs objectAtIndex:j])[email protected]"pack name";
}
NSLog(@"*** NIL sample pack name=%@",((Pack *)[((Deck *)[globDat.allDecks objectAtIndex:0]).packs objectAtIndex:0]).packName);
//always returns null
구조를 조작하는 것이 꽤 번거롭습니다. 이 데이터를 관리하는 가장 좋은 방법입니까?
또한 인코딩이 포함 된 배열 (팩 및 카드)을 저장하지 않는 것 같습니다.
감사합니다 - 내가 코어 데이터로 살펴 보겠습니다. 지금 당장은 나머지 코드를 실행하려고 노력 중이므로 지금은 작동합니다. ARC를 사용하고 있으므로 "자체"가 필요하지 않습니다 (다른 2 개의 앱에서 잘 작동 함). – wayneh