2016-08-22 2 views
0

私はlevel0、level1、level2という名前の3つのplistファイルを持っています。このplistはすべて同じ構造を持ち、変数と配列から構成されています。私はこのplistからのデータで私のアプリを初期化する。plistからの辞書

+(instancetype)levelWithNum:(int)levelNum; { 
    NSString* fileName = [NSString stringWithFormat:@"level%i.plist", levelNum]; 
    NSString* levelPath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:fileName]; 
    NSDictionary *levelDic = [NSDictionary dictionaryWithContentsOfFile:levelPath]; 
    NSAssert(levelDic, @"level no loaded"); 
    Level *l = [[Level alloc]init]; 
    l.coinsPerLvl = [levelDic[@"coinsPerLvl"] integerValue]; 
    l.words = levelDic[@"words"]; 
    return l; 
} 

ここでは、plistを1つだけ使用して3つの辞書を追加することにしました。私はplistファイルを使用する上記の例のようなplistから1つの辞書だけを読むことができます。

助けを借りてタンク!

enter image description here

+0

levels.plistでの問題は何ですか? –

答えて

1

あなただけの中間変数のthatsがルート辞書を表し必要と例えば、根の辞書からレベルの辞書を抽出します。

+(instancetype)levelWithNum:(int)levelNum; { 

    NSString  *levelsPlist = [[NSBundle mainBundle] pathForResource:@"levels" ofType:@"plist"]; 
    NSDictionary *rootDict  = [NSDictionary dictionaryWithContentsOfFile: levelsPlist]; 
    NSString  *levelKey  = [NSString stringWithFormat:@"level%i", levelNum]; 

    NSDictionary *levelDic = rootDict[levelKey]; 

    NSAssert(levelDic, @"level no loaded"); 

    Level *l = [[Level alloc]init]; 
    l.coinsPerLvl = [levelDic[@"coinsPerLvl"] integerValue]; 
    l.words = levelDic[@"words"]; 
    return l; 
} 

はそれが役立つ願っています。

+0

ありがとう!それは私に多くの助けになる! –

0

plistルートを配列として作成します(下記参照)。あなたはレベル情報を簡単に得ることができます。

サンプルコード

+(void)levelWithNum:(int)levelNum { 

NSString* fileName = @"level.plist"; 
NSString* levelPath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:fileName]; 
NSArray *levelsArray = [NSArray arrayWithContentsOfFile:levelPath]; 
NSAssert(levelsArray, @"level no loaded"); 


Level *l = [[Level alloc]init]; 
l.coinsPerLvl = [[levelsArray objectAtIndex:levelNum][@"coinsPerLvl"] integerValue]; 
l.words = [[levelsArray objectAtIndex:levelNum][@"words"] integerValue]; 
return l; 
} 

sample plist screen shot

関連する問題