2013-01-22 27 views
-1

在我的代碼中,我使用JSONValue來獲取NSDictionary。如何從JSONValue的NSDictionary中獲取pickerView的值?

詞典是這樣的:

{ 
    {ID = 0, NAME = "Patrick"}, 
    {ID = 1, NAME = "Ted"}, 
    {ID = 2, NAME = "John"}, 
    {ID = 3, NAME = "Allen"}, 
    {ID = 4, NAME = "David"} 
} 

我想在下面的委託函數返回相應的ID名稱:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

什麼是最有效的方式來做到這一點?

+1

注意,這已經無關JSON。你有一個包含5個NSDictionaries的NSArray。如果您在此階段將其視爲「JSON」,那麼您只會迷惑自己。 –

+0

JSON中的數組用括號表示:[]。 –

回答

0

例如:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    // Assuming that you have just one component 
    return [jsonDict[row] objectForKey: @(row) ]; 
} 
+0

@(row)是什麼意思?我知道@「字符串」是解決Objective-C中字符串的唯一方法,但行只是一個NSInteger – Ted

+0

它表示'[NSNumber numberWithInt:row];' –

+0

不是jsonDict [x]字典本身? – Ted