从类中过滤数组

时间:2017-09-16 16:35:20

标签: json swift filter

我试图过滤我的类数组,但我一直收到错误,我不知道为什么

这是我的代码:

var shops = [Shops]()

当我从服务器获取数据并保存时

 let desgin = subJson["desgin"].stringValue

 let familiy_id = subJson["id"].stringValue


 var info = Shops(shopname: shopName, Logo: logoString, family_id: familiy_id , design : desgin )

self.shops.append(info)
  

元组字符串json的值没有成员设计

我正试图用设计过滤商店。 Shops是我保存JSON数据的地方

class Shop {
    ...
 var design: String?
    ...
}

1 个答案:

答案 0 :(得分:3)

我认为问题就在这一行

 var shops = [Shops]()

和这一行

 Shops(shopname: shopName, Logo: logoString, family_id: familiy_id , design : desgin )

您正在使用Shops创建对象,并且您正在根据Shop类进行过滤

您的代码应为:

var shops = [Shop]()


 let desgin = subJson["desgin"].stringValue

 let familiy_id = subJson["id"].stringValue


 var info = Shop(shopname: shopName, Logo: logoString, family_id: familiy_id , design : desgin )

self.shops.append(info)

let filteredArr = self.shops.filter { $0.design != nil }

这是您的班级Shop

class Shop {
    ...
 var design: String?
    ...
}