2016-08-22 4 views
0

나는이 plist의 이름이 level0, level1, level2 인 3 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를 하나만 사용하여 3 개의 사전을 추가하기로 결정했습니다. plist 파일을 사용하는 위의 예제처럼 plist에서 하나의 사전 만 읽을 수 있습니까?

도움이 필요하시면 탱크를 이용하십시오! 당신은 단지 중간 변수 그게 필요

enter image description here

+0

levels.plist의 문제가 무엇입니까? –

답변

1

루트 사전을 나타내고 루트 사전에서 레벨 사전, 예컨대 추출 :

+(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