我需要爲我正在寫的閃存卡應用程序存儲多級數據,並且我可以使用一些幫助來計算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