2013-10-26 43 views
0

基本上我需要得到一個隨機字母和與來自plist的那封信相關的點。當我得到這個工作,我會是 enter image description here如何從嵌套在plist文件字典中的數組中訪問隨機值?

: 我真的不知道我有我的plist在儘可能最好的方式做到這一點成立,我從來沒有用的plist工作過將每個字母相關的點添加到plist中。我只是創建了一個基本的字平鋪遊戲來嘗試和學習sprite套件。

到目前爲止,我已經能夠訪問plist文件,但沒有得到任何運氣得到一個隨機的信,它是Points.I'm得到我嘗試的一切的各種錯誤。

Here's my code so far to access the plist: 
     NSString* fileName = @"letters.plist"; 
     NSString* filepath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; 

     NSDictionary* lettersPlist = [NSDictionary dictionaryWithContentsOfFile:filepath]; 
     NSDictionary* letters = lettersPlist[@"letters"]; 

那麼我怎樣才能得到一個隨機信,它是從這個plist點?有沒有更好的方法來做到這一點?

回答

4

只需獲得一個介於0和letters陣列中的項目數之間的隨機數,然後取出該信息。例如,繼續您的代碼:

u_int32_t bound = (u_int32_t)[letters count]; 
NSDictionary* randomLetterInfo = letters[arc4random_uniform(bound)]; 
NSString* randomLetter = randomLetterInfo[@"Letter"]; 
NSString* points = randomLetterInfo[@"Points"]; // points are a string in your plist 
+0

不錯!我只是做了你的建議,然後不得不改變字母指針到一個NSArray,並做了訣竅。 – Scott

+0

@Scotty對不起,我甚至沒有看到它是你的示例代碼中的'Dictionary',因爲它是你的plist中的一個'Array';)對不起, –

+0

沒有問題。由於我收到的錯誤非常明顯,我需要做的事情。 – Scott