[英]Extracting an NSDictionary from within another NSDictionary
我的應用程序具有一個NSDictionary
其中包含許多其他NSDictionary
。 如果我打印出此詞典,則其內容如下:
oxip = {
created = "2014-02-10 14:42:59";
lastMsgId = "";
requestTime = "1.6434";
response = {
code = 001;
debug = "";
message = success;
request = getHierarchyByMarketType;
text = "\n";
williamhill = {
class = {
id = 1;
maxRepDate = "2014-02-10";
maxRepTime = "07:31:48";
name = "UK Football";
text = "\n";
type = (
{
id = 2;
lastUpdateDate = "2013-12-26";
lastUpdateTime = "13:32:54";
market = (
{
betTillDate = "2014-02-15";
betTillTime = "15:00:00";
date = "2014-02-15";
id = 140780553;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = "Queen of the South v Dundee - Match Betting";
participant = (
{
handicap = "";
id = 496658381;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = Dundee;
odds = "11/8";
oddsDecimal = "2.38";
text = "\n\n\n\n\n\n";
},
{
handicap = "";
id = 496658380;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = Draw;
odds = "5/2";
oddsDecimal = "3.50";
text = "\n";
},
{
handicap = "";
id = 496658379;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = "Queen of the South";
odds = "11/8";
oddsDecimal = "2.38";
text = "\n";
}
);
text = "\n";
time = "15:00:00";
}
我的應用程序以以下名稱到達NSDictionary
的最佳方法是:
名稱=“南方女王v鄧迪-比賽投注”
不需要遍歷每個單獨的詞典並找到其鍵對象?
您可以為此使用valueForKeyPath
。 它接受由點分隔的路徑。 例:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/u/1365846/21680479.json"]]
options:0
error:nil];
NSLog(@"%@", [dict valueForKeyPath:@"response.williamhill.class.type.market.name"]);
這取決於字典的表示形式。 如果williamhill
部件在變化,那么它當然不起作用。
如果沒有遍歷,就無法在地圖數據類型(在本例中為NSDictionary
)中獲取引用。 考慮一下此問題的簡化版本:您有一個包含N個元素的鏈表,並且希望到達第N個元素。 因為這是一個鏈表,所以您必須遍歷所有其他N-1個節點才能獲取最后的引用。
NSDictionary
是基於哈希的數據類型,其中存儲了鍵和值。 在您描述的情況下,您沒有對嵌套對象(NSDictionary本身)的引用,因此您還必須遍歷包含該對象的所有字典。
希望這可以幫助您指出正確的方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.