如何使用部分访问 UITableView 中的数据模型 [String: [String]]?
How to access a Data Model [String: [String]] in UITableView with sections?
我正在尝试准备我的数据模型,以便它可以在带有部分的 UITableView 中使用。
var folderHolder: [String: [String]]?
folderHolder = ["Projects": ["All", "Recent"], "Smart Folders": ["Folder 1", "Folder 2", "Folder 3"]]
如何通过索引访问该字典中的键和对象(根据 UITableView 的需要)
我在 playground 上尝试了这个,但卡住了。感谢您对此的帮助。
// Need number of Keys
// Expected result: 2
folderHolder!.count
// Need number of elements in Key
// Expected: All and Recent are in Projects, so 2 would be expected
folderHolder!["Projects"]
folderHolder!["Projects"]!.count
// How can I get this result by stating the index, e.g. writing 1 as a parameter instead of "Smart Folders"
folderHolder![1]!.count
// Need specific element
// Input parameter: Key index, Value index
// Expected: "Folder 2"
folderHolder![1]![1]
// I don't know why it only works when I state the key explicitly.
folderHolder!["Smart Folders"]![1]
字典的设置方式不能像索引数组那样对它们进行索引。由于字典的 Key: Value 性质,顺序并不重要,因此像这样的下标:folderHolder[1]
将不起作用。像这样的索引只适用于数组,其中顺序很重要,因此需要维护。
Swift 文档 here 指出:
A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order.
经过更多研究找到了解决方案:
字典键需要转换成数组。可以通过索引(UITableView 的部分)和 return 键的名称访问数组项。键的名称可用于访问字典的值(UITableView 的行)。
这里是正确的游乐场数据作为参考:
var folderHolder: [String: [String]]?
folderHolder = ["Projects": ["All", "Recent"], "Smart Folders": ["Folder 1", "Folder 2", "Folder 3"]]
let folderHolderArray = Array(folderHolder!.keys)
// Need number of Keys
// Expected: 2
folderHolder!.count
folderHolderArray.count
// Need number of elements in Key
// Expected: All and Recent are in Projects, so 2 would be expected
folderHolder!["Projects"]
folderHolder!["Projects"]!.count
// How can I get this result by stating the index, e.g. writing 1 as a parameter instead of "Smart Folders"
folderHolderArray[1]
// Need specific element
// Input parameter: Key index, Value index
// Expected: "Folder 2"
//folderHolder![1]![1]
let folderHolderSection = folderHolderArray[1]
let folders = folderHolder![folderHolderSection]
let folder = folderHolder![folderHolderSection]![1]
我正在尝试准备我的数据模型,以便它可以在带有部分的 UITableView 中使用。
var folderHolder: [String: [String]]?
folderHolder = ["Projects": ["All", "Recent"], "Smart Folders": ["Folder 1", "Folder 2", "Folder 3"]]
如何通过索引访问该字典中的键和对象(根据 UITableView 的需要)
我在 playground 上尝试了这个,但卡住了。感谢您对此的帮助。
// Need number of Keys
// Expected result: 2
folderHolder!.count
// Need number of elements in Key
// Expected: All and Recent are in Projects, so 2 would be expected
folderHolder!["Projects"]
folderHolder!["Projects"]!.count
// How can I get this result by stating the index, e.g. writing 1 as a parameter instead of "Smart Folders"
folderHolder![1]!.count
// Need specific element
// Input parameter: Key index, Value index
// Expected: "Folder 2"
folderHolder![1]![1]
// I don't know why it only works when I state the key explicitly.
folderHolder!["Smart Folders"]![1]
字典的设置方式不能像索引数组那样对它们进行索引。由于字典的 Key: Value 性质,顺序并不重要,因此像这样的下标:folderHolder[1]
将不起作用。像这样的索引只适用于数组,其中顺序很重要,因此需要维护。
Swift 文档 here 指出:
A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order.
经过更多研究找到了解决方案:
字典键需要转换成数组。可以通过索引(UITableView 的部分)和 return 键的名称访问数组项。键的名称可用于访问字典的值(UITableView 的行)。
这里是正确的游乐场数据作为参考:
var folderHolder: [String: [String]]?
folderHolder = ["Projects": ["All", "Recent"], "Smart Folders": ["Folder 1", "Folder 2", "Folder 3"]]
let folderHolderArray = Array(folderHolder!.keys)
// Need number of Keys
// Expected: 2
folderHolder!.count
folderHolderArray.count
// Need number of elements in Key
// Expected: All and Recent are in Projects, so 2 would be expected
folderHolder!["Projects"]
folderHolder!["Projects"]!.count
// How can I get this result by stating the index, e.g. writing 1 as a parameter instead of "Smart Folders"
folderHolderArray[1]
// Need specific element
// Input parameter: Key index, Value index
// Expected: "Folder 2"
//folderHolder![1]![1]
let folderHolderSection = folderHolderArray[1]
let folders = folderHolder![folderHolderSection]
let folder = folderHolder![folderHolderSection]![1]