2012-04-26 57 views
0

我是新來的plists,我真的需要使用一個。我擁有的是一個plist,用兩個詞典存儲不同的數字。我需要獲得存儲的那個數字,並將其作爲一個整數。此過程將從名爲「readPlist」的方法運行。整數從.plist

plist被稱爲'properties.plist'。第一本字典被稱爲'敵人'。它包含各種其他字典,其名稱將存儲在名爲「SpriteType」的NSMutableString中。數字的名稱將具有格式'L - %d',其中%d是一個名爲'LevelNumber'的整數。

如果可能的話,有人可以給我關於如何使用該信息以及上面的字典的名稱獲取該整數的代碼。

我環顧四周如何訪問plists,但人們已經顯示的代碼不起作用。

在此先感謝。

編輯:太讓它更容易理解,這是我的plist。我理想中的一個整數,稱爲「SpriteNumber」等於「的值L - %d」

properties.plist

回答

2

如果你讀你的plist中的內容轉換成字典(我不會告訴你如何做到這一點,但this是我經常參考的教程),那麼這是一個與[[myDictionary objectForKey:@"key"]stringValue];級別的關鍵字不同的問題。然後,使用NSString的非常有用的-stringByReplacingOccurencesOfString:withString:來擺脫「L - 」部分,只獲得一個數值。最後,從[myString intValue]的字符串中獲得一個整數。

+0

我剛剛添加了一張圖片,讓我想要的更清晰些。 – akuritsu 2012-04-26 04:36:48

+0

啊!所以關鍵就是'L - %d',而且對象已經是一個整數了。我的朋友,這讓你的生活變得更加輕鬆。 – CodaFi 2012-04-26 04:38:27

+0

好的,所以我有代碼,但在部分: SpriteNumber = [[savedStock objectForKey:@「L - %d」,LevelNumber] intValue]; 我收到一個錯誤,說在LevelNumber上'太多的參數方法調用,預期1,有2'。 你知道一種解決這個問題的方法嗎? – akuritsu 2012-04-26 05:15:36

0

好,最簡單的方法是這樣的:

-(int) getMosquitoCountForLevel:(int) level { 
    int mosquitoCount=0; 
    NSString *gsFile = @"whateverFullyQualifiedFileNameYourPlistIs"; 
    NSDictionary* definitions = [NSDictionary dictionaryWithContentsOfFile:gsFile]; 
    NSDictionary* mosquitos = [definitions objectForKey:@"Mosquito"]; 
    if(mosquitos) { 
     NSString *levelKey = [NSString stringWithFormat:@"L - %d",level]; 
     NSNumber *mosquitoCountAsNumber = [mosquitos objectForKey:levelKey]; 
     if(mosquitoCountAsNumber) { 
     mosquitoCount=[mosquitoCountAsNumber intValue]; 
     } else { 
     CCLOGERROR(@"%@ - Mosquito definitions in %@ does not contain a en entry for level %@.",self.class,gsFile,levelKey); 
     } 
    } else { 
     CCLOGERROR(@"%@ - file %@ does not contain a Mosquito dictionary.",self.class,gsFile); 
    } 
    return mosquitoCount; 
} 

這個編譯,但不能與實際數據測試。

+0

那麼我想要的號碼叫做mosquitoCount? – akuritsu 2012-04-28 04:49:51

+0

是的,例如,此方法將返回等級15,16和17的整數9與上面的plist – YvesLeBorg 2012-04-28 12:20:43