我需要为我正在写的闪存卡应用程序存储多级数据,并且我可以使用一些帮助来计算1)如何管理数据和2)如何存储它。如何创建一个用于存储多级数据的类?
数据被分解如下: 一个)卡包含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”举行甲板,又包含卡,但我还没有能够得到这个工作(没有错误,但“包名”总是空):
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,因此不需要“自我”(在其他两个应用程序中运行良好)。 – wayneh 2012-03-20 17:28:08