簡體   English   中英

如何使用objectForKey訪問NSDictionary中的數據

[英]How to access data in a NSDictionary with objectForKey

我正在嘗試訪問字典中的數據。 當它是一個字符串時,我可以訪問數據:

cell.textLabel.text = [[item objectForKey:@"merchant"]objectForKey:@"name"];

但不確定NSNumber的語法:

NSNumber* latFromTable = [[displayItems objectAtIndex:indexPath.row]
                                           [objectForKey:@"coords"]objectForKey:@"lat"];

我知道我在上面的代碼中使用NSStrings,將無法正常工作。 我如何將其更改為與NSNumber一起使用?

字典中的數據如下所示。 objectForKey是“ coords”。 我想訪問項目1和2(經緯度)

 "coords": {
        "lat": 52.5032,
        "lng": 13.3445

謝謝你的幫助。

以下代碼的問題是括號不匹配:

NSNumber* latFromTable = [[displayItems objectAtIndex:indexPath.row]
                                           [objectForKey:@"coords"]objectForKey:@"lat"];

因此,將其更改為:

float latFromTable = (float)[[[displayItems objectAtIndex:indexPath.row]
                                           objectForKey:@"coords"]objectForKey:@"lat"];
NSNumber *value = [NSNumber numberWithFloat:latFromTable];

像這樣嘗試:

float latFromTable = [[[displayItems objectForIndex:indexPath.row]
                                       [objectForKey:@"coords"]objectForKey:@"lat"] floatValue];

字典中的鍵值對稱為條目。 每個條目都包含一個代表鍵的對象和另一個代表鍵值的對象。

NSInteger不是對象(它只是一個int值)。

所以你做不到

--- ADDED如schubam所建議的,您可以接收NSString並調用floatValue。

--- ADDED2我已經更准確地閱讀了日志。 您有一個具有經度和緯度屬性的對象。 因此,它不是第二級字典。 嘗試接收接口名稱並獲取這些接口的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM