排序任意对象Swift 3的数组 [英] sort array of anyobject Swift 3

查看:212
本文介绍了排序任意对象Swift 3的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试排序任何对象的数组,但无法做到这一点。我从AnyObject格式的Parse数据库中获取一些数据。根据下面的数据,我想通过NAME对AnyObject数组进行排序。以下是我的代码 -

  let sortedArray =(myArray as![AnyObject])。sorted(by:{(dictOne,dictTwo ) - > Bool in 
let d1 = dictOne [NAME]!as AnyObject; //此行给出错误下标的模糊使用
let d2 = dictTwo [NAME]!as AnyObject; //这一行给出错误下标的模糊使用

返回d1< d2
})

myArray看起来像这样 -

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

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

解决方案

为什么要将数组转换为 [AnyObject] 而不是将数组转换为 [[String:Any]] 意味着数组 Dictionary> / code>并告诉编译器该数组包含字典作为对象。

 如果让array = myArray为? [[String:Any]] {
let sortedArray = array.sorted(by:{$ 0 [NAME] as!String< $ 1 [NAME] as!String})
}

注意:由于您有 NAME 键与 String 在数组的每个字典的值我有力包装它与下标。


I am trying to sort an array of anyobject, but not able to do it.I get some data from Parse database in AnyObject format. As per below data, I want to sort this AnyObject array by "NAME". Below is my code -

  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 looks like this -

 {
            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;
        }

Any help would be much appreciated. Thanks!

解决方案

Why are converting array to [AnyObject] instead of that convert the array to the [[String:Any]] means Array of Dictionary and tell the compiler that array contains Dictionary as objects.

if let array = myArray as? [[String:Any]] {
   let sortedArray = array.sorted(by: { $0["NAME"] as! String < $1["NAME"] as! String })
}

Note: As of you are having NAME key with String value in your each dictionaries of array I have force wrapped it with the subscript.

这篇关于排序任意对象Swift 3的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆