2012-03-20 45 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

相關問題