NSDictionary
讓我困惑。它有點像我收集的結構和數組的混合體。我正在瀏覽iOS編程書,它讓我們把一個JSON對象放入一個NSDictionary
。對於具有「標題」鍵的對象,所有返回都是正確的,但對於upcoming
類,在upcoming
內有變量。在我的腦海中,這是如何構建NSDictionary
。我如何訪問NSDictionary的另一個成員內的成員,同時保持成員之間的鏈接
struct classes {
NSString *title;
struct upcoming[] {
NSString *end_date;
NSString *start_date;
NSString *instructor;
}
}
}
我一直試圖「通過使用變化的for
循環內S:
NSLog(@"%@", [course objectForKey:@"upcoming.start_date"]);
的我這樣做的目的是要追加的title
字符串」檢索start_date
s表示在upcoming
類別中的值爲start_date
。喜歡的東西...
NSString *value = course[@"title"];
if ([course [email protected]"upcoming"] [value stringByAppendingString:
@" course[@"upcoming.start_date"];
NSLog(@"%@", value);
怎樣讓這個如果沒有爲upcoming
一個或多個值,那麼它會通過將字符串顯示的要不然也不會做任何顯示的起始日期值?顯然upcoming.start_date
是不是做到這一點!和start_date
本身沒有鏈接到相關title
。我需要它們都被鏈接來追加適當的字符串。
解決方案:
NSString *title = [course objectForKey:@"title"];
NSArray *upcomingCourses = [course objectForKey:@"upcoming"];
if (upcomingCourses.count > 0
NSString *showDate = [NSString stringWithFormat:@"%@. Next Class Date: %@"
title, [[upcomingCourses objectAtIndex:0] objectForKey:@"start_date"]];
NSLog(@"%@", showDate);
}
else NSLog(@"%@", title);
發佈NSDictionary
從NSLog
:
courses = (
{
title = "Advanced iOS Bootcamp";
upcoming = (
{
"end_date" = "2014-04-04";
instructors = Blocksom;
location = "BNR West, California";
"start_date" = "2014-03-31";
},
{
"end_date" = "2014-05-16";
instructors = Dalrymple;
location = "BNR West, California";
"start_date" = "2014-05-12";
},
{
"end_date" = "2014-06-20";
instructors = Ward;
location = "Atlanta, Georgia";
"start_date" = "2014-06-16";
}
);
url = "https://training.bignerdranch.com/classes/advanced-ios-bootcamp";
},
{
title = "Beginning iOS (iPhone/iPad)";
upcoming = (
{
"end_date" = "2014-04-18";
instructors = Mathias;
location = "BNR West, California";
"start_date" = "2014-04-12";
},
{
"end_date" = "2014-05-02";
instructors = "Keur, Ward";
location = "Atlanta, Georgia";
"start_date" = "2014-04-26";
},
{
"end_date" = "2014-05-23";
instructors = Christopher;
location = "Atlanta, Georgia";
"start_date" = "2014-05-17";
},
{
"end_date" = "2014-06-06";
instructors = Christopher;
location = "BNR West, California";
"start_date" = "2014-05-31";
},
{
"end_date" = "2014-07-25";
instructors = Ritchie;
location = "BNR West, California";
"start_date" = "2014-07-19";
},
{
"end_date" = "2014-08-01";
instructors = "Usiyan, Keur";
location = "Atlanta, Georgia";
"start_date" = "2014-07-26";
},
{
"end_date" = "2014-09-19";
instructors = Ritchie;
location = "BNR West, California";
"start_date" = "2014-09-13";
}
);
url = "https://training.bignerdranch.com/classes/beginning-ios";
},
{
title = "Mobile Design Bootcamp";
upcoming = (
{
"end_date" = "2014-06-13";
instructors = "Harper, Porter";
location = "BNR West, California";
"start_date" = "2014-06-09";
}
);
url = "https://training.bignerdranch.com/classes/mobile-design-bootcamp";
},
{
title = "Python Programming";
upcoming = (
{
"end_date" = "2014-04-24";
instructors = Cassell;
location = "Atlanta, Georgia";
"start_date" = "2014-04-21";
},
{
"end_date" = "2014-06-19";
instructors = Cassell;
location = "BNR West, California";
"start_date" = "2014-06-16";
}
);
url = "https://training.bignerdranch.com/classes/python-programming";
},
{
title = "Beginning iOS Game Development";
upcoming = (
{
"end_date" = "2014-06-20";
instructors = Strougo;
location = "BNR West, California";
"start_date" = "2014-06-16";
},
{
"end_date" = "2014-09-26";
instructors = Strougo;
location = "BNR West, California";
"start_date" = "2014-09-22";
}
);
url = "https://training.bignerdranch.com/classes/beginning-ios-game-development";
},
{
title = "Android Bootcamp";
upcoming = (
{
"end_date" = "2014-05-16";
instructors = Phillips;
location = "Atlanta, Georgia";
"start_date" = "2014-05-12";
},
{
"end_date" = "2014-06-20";
instructors = Marsicano;
location = "Atlanta, Georgia";
"start_date" = "2014-06-16";
},
{
"end_date" = "2014-07-11";
instructors = Stewart;
location = "BNR West, California";
"start_date" = "2014-07-07";
},
{
"end_date" = "2014-10-03";
instructors = Phillips;
location = "BNR West, California";
"start_date" = "2014-09-29";
}
);
url = "https://training.bignerdranch.com/classes/android-bootcamp";
},
{
title = "HTML5 Apps with jQuery";
upcoming = (
{
"end_date" = "2014-05-16";
instructors = Aquino;
location = "Atlanta, Georgia";
"start_date" = "2014-05-12";
},
{
"end_date" = "2014-06-13";
instructors = Gandee;
location = "Atlanta, Georgia";
"start_date" = "2014-06-09";
},
{
"end_date" = "2014-08-01";
instructors = Aquino;
location = "BNR West, California";
"start_date" = "2014-07-28";
}
);
url = "https://training.bignerdranch.com/classes/html5-apps-with-jquery";
},
{
title = "Ruby on the Server";
upcoming = (
{
"end_date" = "2014-06-13";
instructors = Stewart;
location = "Atlanta, Georgia";
"start_date" = "2014-06-09";
}
);
url = "https://training.bignerdranch.com/classes/ruby-on-the-server";
},
{
title = "iOS Bootcamp - Fast Track";
upcoming = (
{
"end_date" = "2014-07-11";
instructors = Usiyan;
location = "Atlanta, Georgia";
"start_date" = "2014-07-07";
}
);
url = "https://training.bignerdranch.com/classes/ios-bootcamp-fast-track";
},
{
title = "Cocoa I Bootcamp";
upcoming = (
{
"end_date" = "2014-07-25";
instructors = Preble;
location = "Atlanta, Georgia";
"start_date" = "2014-07-21";
}
);
url = "https://training.bignerdranch.com/classes/cocoa-i-bootcamp";
},
{
title = "OpenGL ES Bootcamp";
upcoming = (
{
"end_date" = "2014-07-25";
instructors = Blocksom;
location = "Atlanta, Georgia";
"start_date" = "2014-07-21";
}
);
url = "https://training.bignerdranch.com/classes/opengl-es-bootcamp";
}
);
}
一個'NSDictionary'是鍵的集(無重複)和值的集合之間的映射。根據您熟悉的其他語言,其結構可能被稱爲「[關聯數組](http://en.wikipedia.org/wiki/Associative_array)」,「哈希表」或「映射」。每個鍵對應一個值;他們是配對的,並且這些配對沒有訂購。 –
你可以把你的字典的實際結構或佈局納入問題嗎?通過NSLog()打印出來就可以了。 –
@JoshCaswell更新。它的長! – Scott