阵列过滤器在swift中

时间:2016-11-09 07:10:19

标签: ios swift nsmutablearray

我想在另一个数量大于零的数组上添加响应。没有迭代我的数组就可以。是他们的任何功能,因为我的数组有很多价值。请帮助任何帮助将不胜感激。

{
            id = 1;
            img = "http://alatiffy.com/admin/uploads/1478601364.png";
            name = "Executive Lunch Tiffy";
            price = 99;
            quantity = 1;
        },
            {
            id = 2;
            img = "http://alatiffy.com/admin/uploads/1478601409.png";
            name = "Deluxe Lunch Tiffy";
            price = 149;
            quantity = 0;
        },
            {
            id = 4;
            img = "http://alatiffy.com/admin/uploads/1478583806.jpg";
            name = "KADHI PAKODA CHAWAL";
            price = 59;
            quantity = 1;
            status = Y;
        },
            {
            id = 2;
            img = "http://alatiffy.com/admin/uploads/1478583832.jpg";
            name = "DAL MAKHNI CHAWAL";
            price = 59;
            quantity = 0;
            status = Y;
        },
            {
            id = 5;
            img = "http://alatiffy.com/admin/uploads/1478583850.jpg";
            name = "SHAHI PANEER CHAWAL";
            price = 59;
            quantity = 0;

 status = Y;
    },

2 个答案:

答案 0 :(得分:1)

  

Swift 3.x

假设您将上述JSON数据保存在类型[String:AnyObject]

的字典数组中

所以你的词典将是

var dict = Dictionary<String,AnyObject>()

您的数组将是上述类型字典的数组

var array = [Dictionary<String,AnyObject>]()

为了过滤数量大于0的数组

let filteredArray = array.filter{$0["quantity"] as! Int > 0}

您的filteredArray将包含所有数量大于零的字典对象。

我建议你制作一个Food或Lunch项目的模型类,并在模型本身上应用过滤器,而不是使用字典。

答案 1 :(得分:-1)

试试这段代码,

   var filteredResults = arr.filter { $0.quantity > 0 }