gpt4 book ai didi

ios - 任何对象 Swift 3 的排序数组

转载 作者:行者123 更新时间:2023-11-28 18:50:29 25 4
gpt4 key购买 nike

我正在尝试对 anyobject 的数组进行排序,但无法做到这一点。我从 Parse 数据库中以 AnyObject 格式获取了一些数据。根据以下数据,我想按“NAME”对这个 AnyObject 数组进行排序。以下是我的代码 -

  let sortedArray = (myArray as! [AnyObject]).sorted(by: { (dictOne, dictTwo) -> Bool in
let d1 = dictOne["NAME"]! as AnyObject; // this line gives error "Ambiguous use of subscript"
let d2 = dictTwo["NAME"]! as AnyObject; // this line gives error "Ambiguous use of subscript"

return d1 < d2
})

myArray 看起来像这样 -

 {
LINK = "www.xxx.com";
MENU = Role;
"MENU_ID" = 1;
NAME = "A Name";
SUBMENU = "XXX";
"Training_ID" = 2;
},
{
LINK = "www.xyz.com";
MENU = Role;
"MENU_ID" = 2;
NAME = "B name";
SUBMENU = "jhjh";
"Training_ID" = 6;
},
{
LINK = "www.hhh.com";
MENU = Role;
"MENU_ID" = 3;
NAME = "T name";
SUBMENU = "kasha";
"Training_ID" = 7;
},
{
LINK = "www.kadjk.com";
MENU = Role;
"MENU_ID" = 5;
NAME = "V name";
SUBMENU = "ksdj";
"Training_ID" = 1;
},
{
LINK = "www.ggg.com";
MENU = Role;
"MENU_ID" = 4;
NAME = "K name";
SUBMENU = "idiot";
"Training_ID" = 8;
},
{
LINK = "www.kkk.com";
MENU = Role;
"MENU_ID" = 6;
NAME = "s name";
SUBMENU = "BOM/ABOM/BSM";
"Training_ID" = 12;
}

任何帮助将不胜感激。谢谢!

最佳答案

它不是[AnyObject](I-have-no-idea 数组),它是字典数组[[String:Any]]。更具体地说,这解决了错误。

在 Swift 3 中,编译器必须知道所有下标对象的具体类型。

let sortedArray = (myArray as! [[String:Any]]).sorted(by: { (dictOne, dictTwo) -> Bool in
let d1 = dictOne["NAME"]! as String
let d2 = dictTwo["NAME"]! as String

return d1 < d2
})

关于ios - 任何对象 Swift 3 的排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42232052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com