2012-03-20 183 views
0

我需要为我正在写的闪存卡应用程序存储多级数据,并且我可以使用一些帮助来计算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 

工作结构非常麻烦。 这是管理这些数据的最佳方式吗?

此外,编码似乎并没有保存嵌入式数组(包和卡)。

回答

1

我会张贴这个答案,虽然它真的是一个意见。

我会使用模型图层的核心数据。你不需要像现在这样处理序列化你的对象图。相反,对象图持久性很大程度上由框架处理。有一条学习曲线 - 对于苹果来说,它不是一个“入门级技术” - 但从长远来看,它将更易于管理。

至于序列化对象图中数组的问题,NSMutableArray符合NSCoding协议;还有一些其他问题。相反的:

packs=[decoder decodeObjectForKey:@"packs"]; 

不要你的意思是:

self.packs = [decoder decodeObjectForKey:@"packs"]; 

packs = [[decoder decodeObjectForKey:@"packs"] retain]; 

(我你不使用ARC假设...)

+0

感谢您的意见 - 我会考虑的核心数据。现在,我只是试图让我的代码的其余部分正常运行,所以现在可以使用。我正在使用ARC,因此不需要“自我”(在其他两个应用程序中运行良好)。 – wayneh 2012-03-20 17:28:08

1

老实说,我会继续你做事的方式。 PackCard未被保存的原因是因为它们每个都需要实现encodeWithCoder:initWithCoder:方法。

Card.h

@interface Card : NSObject 

@property (nonatomic,retain)NSString *primaryPhrase; 
@property (nonatomic,retain)NSString *secondaryPhrase; 

@end 

Card.m

@implementation Card 

@synthesize primaryPhrase, secondaryPhrase; 

-(id)init{ 
    if(self=[super init]){ 
    } 
    return self; 
} 

-(void)encodeWithCoder:(NSCoder*)encoder{ 
    [encoder encodeObject:primaryPhrase forKey:@"primaryPhrase"]; 
    [encoder encodeObject:secondaryPhrase forKey:@"secondaryPhrase"]; 
} 

-(id)initWithCoder:(NSCoder*)decoder{ 
    if(self=[super init]){ 
     primaryPhrase=[decoder decodeObjectForKey:@"primaryPhrase"]; 
     secondaryPhrase=[decoder decodeObjectForKey:@"secondaryPhrase"]; 
    } 
    return self; 
} 

@end 

Pack.h

@interface Pack : NSObject 

@property (nonatomic,retain)NSMutableArray *cards; 
@property (nonatomic,retain)NSString  *packName; 
@property (nonatomic,assign)BOOL   isInUse; 

@end 

Pack.m

@implementation Pack 

@synthesize packName, cards, isInUse; 

-(id)init{ 
    if(self=[super init]){ 
     self.isInUse=YES; 
    } 
    return self; 
} 

-(void)encodeWithCoder:(NSCoder*)encoder{ 
    [encoder encodeObject:packName forKey:@"packName"]; 
    [encoder encodeObject:cards forKey:@"cards"]; 
    [encoder encodeObject:[NSNumber numberWithBool:isInUse] forKey:@"isInuse"]; 
} 

-(id)initWithCoder:(NSCoder*)decoder{ 
    if(self=[super init]){ 
     packName=[decoder decodeObjectForKey:@"packName"]; 
     cards=[decoder decodeObjectForKey:@"cards"]; 
     isInUse=[[decoder decodeObjectForKey:@"isInUse"] boolValue]; 
    } 
    return self; 
} 

@end 

甲板。^ h

@interface Deck : NSObject <NSCoding> 

@property (nonatomic,retain)NSMutableArray *packs; 
@property (nonatomic,retain)NSString  *deckName; 
@property (nonatomic,retain)NSString  *primaryLang; 
@property (nonatomic,retain)NSString  *secondaryLang; 

@end 

Deck.m

#import "Deck.h" 
@implementation Deck 

@synthesize packs, deckName, primaryLang, secondaryLang; 

-(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; 
} 
+0

我试过在每个类中使用编码,但似乎没有改变。对于这个问题,我很难让'Pack'在'Deck'内使用 - 看到我的帖子靠近底部。 – wayneh 2012-03-20 18:09:42

+0

看起来你可能没有将'allDecks'变量设置为一个新数组。 – FreeAsInBeer 2012-03-20 18:15:36

+0

我在使用它之前在其他地方分配/初始化阵列。我更新了我的帖子以显示我用来存储一些测试数据的实际循环。我能找回的只有'deckName'(甚至在保存之前)。 – wayneh 2012-03-20 20:54:58