我正在开发 swift,并且我有一系列字典,例如,
[ {"added": 2017-12-24, "first_name": Abdullah, "email": [email protected], "Last_name": Jaleel, "place": India, "salutation":Mr}, {"added": 2017-12-24, "first_name": Catherine, "email": [email protected], "Last_name": Rose, "place": India, "salutation":Mrs}, {"added": 2017-12-24, "first_name": Alok, "email": [email protected], "Last_name": Raj, "place": India, "salutation":Mr}, {"added": 2017-12-24, "first_name": Darwin, "email": [email protected], "Last_name": Jose, "place": India, "salutation":Mr}].
在这里,我想根据名字的字母顺序列出 UITableView 中的所有发言者。我可以按字母顺序对字典数组进行排序,但问题是我想在 UITableViewSection 中显示字母表,并且该部分仅包含以该字母表开头的名称(如 iPhone 的联系人屏幕)。
{ a: [{"added": 2017-12-24, "first_name": Abdullah, "email": [email protected], "Last_name": Jaleel, "place": India, "salutation":Mr}, {"added": 2017-12-24, "first_name": Alok, "email": [email protected], "Last_name": Raj, "place": India, "salutation":Mr}], c: [{"added": 2017-12-24, "first_name": Catherine, "email": [email protected], "Last_name": Rose, "place": India, "salutation":Mrs}], d:[{"added": 2017-12-24, "first_name": Darwin, "email": [email protected], "Last_name": Jose, "place": India, "salutation":Mr}] }.
是否可以像上面那样对字典数组进行分组?否则如何在 UITableViewSection 中按字母顺序列出所有发言者。请帮助我。
最佳答案
Is it possible to group the array of dictionary as above?
let users = [
["added": "2017-12-24", "first_name": "Abdullah", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0c0f1e1a141a0d4e3f1a071e120f131a511c1012" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Jaleel", "place": "India", "salutation": "Mr"],
["added": "2017-12-24", "first_name": "Catherine", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71020110141a140340311409101c011d145f121e1c" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Rose", "place": "India", "salutation": "Mrs"],
["added": "2017-12-24", "first_name": "Alok", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15666574707e70672455706d74786579703b767a78" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Raj", "place": "India", "salutation": "Mr"],
["added": "2017-12-24", "first_name": "Darwin", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f2f1e0e4eae4f3b0c1e4f9e0ecf1ede4afe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>", "Last_name": "Jose", "place": "India", "salutation": "Mr"]
]
func firstCharOfFirstName(_ aDict: [String:String]) -> Character {
return aDict["first_name"]!.first!
}
let groupedUsers = Dictionary(grouping: users, by: firstCharOfFirstName)
print(groupedUsers)
--output:--
["C":
[["added": "2017-12-24", "Last_name": "Rose", "place": "India", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20535041454b455211604558414d504c450e434f4d" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Catherine", "salutation": "Mrs"]],
"D":
[["added": "2017-12-24", "Last_name": "Jose", "place": "India", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53202332363836216213362b323e233f367d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Darwin", "salutation": "Mr"]],
"A":
[["added": "2017-12-24", "Last_name": "Jaleel", "place": "India", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b3b0a1a5aba5b2f180a5b8a1adb0aca5eea3afad" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Abdullah", "salutation": "Mr"],
["added": "2017-12-24", "Last_name": "Raj", "place": "India", "email": "spa<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda8a6a8bffc8da8b5aca0bda1a8e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>", "first_name": "Alok", "salutation": "Mr"]]]
然后您可以根据需要对每个组进行排序。
关于ios - 如何在 UITableView 中按字母顺序对字典数组进行排序和分类,作为 swift 3.0 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48002764/