我需要在这个JSON中实现搜索,我试过这样做,但它给了我错误。下面是我映射的代码和模态类。
在尝试了所有可能的方法之后,我仍然无法解决此错误。
我的搜索代码:
self.company = self.companyDetails.filter{ _ in self.companyDetails.company_name.range(of: searchKey) }
显示错误:
类型'[companiesDetail]'的值没有成员'company_name'
Alamofire.request("http://34.210.142.70/isdental/api/v2.0/home/companies", method: .get, parameters: nil, encoding: URLEncoding.default, headers: params).responseObject { (response:DataResponse<ContactRealm>) in
let ContactsRes = response.result.value
self.companyDetails = ContactsRes?.companies as! [companiesDetail]
self.company = ContactsRes?.companies as! [companiesDetail]
}
以下是我的JSON:
{
"error": false,
"message": "Companies fetched succesfully",
"companies": [
{
"company_id": 1,
"company_name": "3M Espe India",
"company_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/companies/3m_espe.png",
"company_website": "http://solutions.3mindia.co.in/wps/portal/3M/en_IN/3M-ESPE-APAC/",
"company_email": "",
"company_description": "",
"company_categories": "General Equipment / Preventives & Oral Care / General & Restorative",
"company_contacts": [
{
"contact_id": 1,
"contact_name": "Zonal Office",
"contact_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/contacts/company_office.png",
"contact_phone": "+91 8022231414",
"contact_location": "Bangalore",
"contact_favourite": false,
"contact_email": "3mespe.in@mmm.com",
"contact_website": "",
"contact_type": 1
},
{
"contact_id": 367,
"contact_name": "3A Dental Company",
"contact_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/contacts/dealer.png",
"contact_phone": "+91 9811082743",
"contact_location": "Delhi",
"contact_favourite": false,
"contact_email": "3adental@sify.com",
"contact_website": "",
"contact_type": 4
},
{
"contact_id": 368,
"contact_name": "Avco Consultancy",
"contact_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/contacts/dealer.png",
"contact_phone": "+91 9811155330",
"contact_location": "Delhi",
"contact_favourite": false,
"contact_email": "avcoindia@gmail.com",
"contact_website": "",
"contact_type": 4
},
{
"contact_id": 369,
"contact_name": "Biotech Agencies",
"contact_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/contacts/dealer.png",
"contact_phone": "+91 9811132274",
"contact_location": "Delhi",
"contact_favourite": false,
"contact_email": "BIOTECHAGENCIES@GMAIL.COM",
"contact_website": "",
"contact_type": 4
},
{
"contact_id": 370,
"contact_name": "Lexical",
"contact_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/contacts/dealer.png",
"contact_phone": "+91 9810015540",
"contact_location": "Delhi",
"contact_favourite": false,
"contact_email": "lexicaldental@gmail.com",
"contact_website": "",
"contact_type": 4
},
{
"contact_id": 371,
"contact_name": "Oramco",
"contact_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/contacts/dealer.png",
"contact_phone": "+91 9810263014",
"contact_location": "Delhi",
"contact_favourite": false,
"contact_email": "oramco@yahoo.com",
"contact_website": "",
"contact_type": 4
},
{
"contact_id": 372,
"contact_name": "Unicorn Denmart",
"contact_image": "https://project-isdental-cammy92.c9users.io/api/images/v2.0/contacts/dealer.png",
"contact_phone": "+91 9810986086",
"contact_location": "Delhi",
"contact_favourite": false,
"contact_email": "achla@unicorndenmart.com",
"contact_website": "",
"contact_type": 4
}]}]}
下面是我的映射类:
import Foundation
import RealmSwift
import ObjectMapper
struct ContactRealm: Mappable {
init?(map: Map) {
}
var companies:Array<companiesDetail>?
mutating func mapping(map: Map) {
self.companies <- map["companies"]
self.filters <- map["filters"]
}
}
struct companiesDetail: Mappable {
init?(map: Map) {
}
var company_id : Int?
var company_name: String?
var company_image : String?
var company_website : String?
var company_email: String?
var company_description : String?
var company_categories : String?
var company_contacts:Array<companycontacts>?
mutating func mapping(map: Map) {
self.company_id <- map["company_id"]
self.company_name <- map["company_name"]
self.company_image <- map["company_image"]
self.company_website <- map["company_website"]
self.company_email <- map["company_email"]
self.company_description <- map["company_description"]
self.company_categories <- map["company_categories"]
self.company_contacts <- map["company_contacts"]
}
}
struct companycontacts: Mappable {
init?(map: Map) {
}
var contact_id : Int?
var contact_name : String?
var contact_image : String?
var contact_phone : String?
var contact_location : String?
var contact_favourite : String?
var contact_email : String?
var contact_website : String?
var contact_type : Int?
mutating func mapping(map: Map) {
self.contact_id <- map["contact_id"]
self.contact_name <- map["contact_name"]
self.contact_image <- map["contact_image"]
self.contact_phone <- map["contact_phone"]
self.contact_location <- map["contact_location"]
self.contact_favourite <- map["contact_favourite"]
self.contact_email <- map["contact_email"]
self.contact_website <- map["contact_website"]
self.contact_type <- map["contact_type"]
}
}