Skip to content

Instantly share code, notes, and snippets.

@mugityaopen
Last active January 8, 2016 02:29
Show Gist options
  • Save mugityaopen/807e7a6f17ca5f6f2e50 to your computer and use it in GitHub Desktop.
Save mugityaopen/807e7a6f17ca5f6f2e50 to your computer and use it in GitHub Desktop.
nest_dictionary_and_array_for_swift
// ①
enum itemLabel:String {
case name = "名前"
case price = "値段"
}
enum sectionLabel:String {
case vegetable = "野菜"
case meat = "肉"
}
// ②
// Dictionary型の配列をArraysで管理
var item0_0 = [itemLabel.name.rawValue:"かぼちゃ",itemLabel.price.rawValue:100]
var item0_1 = [itemLabel.name.rawValue:"レタス",itemLabel.price.rawValue:100]
var data0 = [item0_0,item0_1]
var item1_0 = [itemLabel.name.rawValue:"豚肉",itemLabel.price.rawValue:100]
var item1_1 = [itemLabel.name.rawValue:"牛肉",itemLabel.price.rawValue:100]
var data1 = [item1_0,item1_1]
// ③
var section = [sectionLabel.vegetable.rawValue:data0,sectionLabel.meat.rawValue:data1]
// ④
print(section) // ["肉": [["名前": 豚肉, "値段": 100], ["名前": 牛肉, "値段": 100]], "野菜": [["名前": かぼちゃ, "値段": 100], ["名前": レタス, "値段": 100]]]
print(section[sectionLabel.vegetable.rawValue]!) // [["名前": かぼちゃ, "値段": 100], ["名前": レタス, "値段": 100]]
print(section[sectionLabel.vegetable.rawValue]![0]) // ["名前": かぼちゃ, "値段": 100]
print(section[sectionLabel.vegetable.rawValue]![0][itemLabel.name.rawValue]!,section[sectionLabel.vegetable.rawValue]![0][itemLabel.price.rawValue]!) // かぼちゃ 100
print(section[sectionLabel.vegetable.rawValue]![1][itemLabel.name.rawValue]!,section[sectionLabel.vegetable.rawValue]![1][itemLabel.price.rawValue]!) // レタス 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment