“[Businessdata]”类型的值没有成员'objectAtIndex'

时间:2016-05-06 12:00:21

标签: ios swift search uisearchbar

我尝试将搜索栏实现到我的表格视图中。但是我在一个函数中得到了这个错误。不知道怎么解决?

Value of type '[Businessdata]' has no member 'objectAtIndex'

我的代码

 var arrDict = [Businessdata]()

    func searchMethod(notification:NSNotification)
    {

        isSearching = true;

        let text:String = notification.userInfo!["text"] as! String;

        arrSearch = [];

        for(var i=0;i<arrDict.count;i++)
        {
            if((arrDict.objectAtIndex(i).objectForKey("name")?.lowercaseString?.containsString(text.lowercaseString)) == true)
            {
                arrSearch.addObject(arrDict.objectAtIndex(i));
            }
        }

        TableViewList.reloadData();

    }

编辑:

import UIKit

class Businessdata: NSObject {

    var BusinessName: String?
    var BusinessEmail: String?
    var BusinessLatLng: NSArray?
    var Address: String?
    var ContactNumber: String?
    var WebsiteUrl: String?
    var Specialities:Array<String>?
    var StoreImages: NSArray?
    var Languages:Array<String>?
    var PaymentMethod:Array<String>?
    var OpenHours: [NSDictionary]?
    var Rating: Float?
    var Updated_date: String?
    var FeaturedBusiness: NSDictionary?

    init(json: NSDictionary)
    {
        self.BusinessName = json["business_name"] as? String
        self.BusinessEmail = json["business_email"] as? String
        self.BusinessLatLng = json["latlng"] as? NSArray
        self.Address = json["location"] as? String
        self.ContactNumber = json["phone_no"] as? String
        self.WebsiteUrl = json["website_url"] as? String
        self.Specialities = json["specialities"] as? Array<String>
        self.StoreImages = json["images"] as? NSArray
        self.Languages = json["languages"] as? Array<String>
        self.PaymentMethod = json["method_payment"] as? Array<String>
        self.OpenHours = json["opening_hours"] as? [NSDictionary]
        self.Rating = json["__v"] as? Float
        self.Updated_date = json["updated_at"] as? String

        if((json["featured_business"]) != nil)
        {
            self.FeaturedBusiness = json["featured_business"] as? NSDictionary
        }
    }

}

这里我发布了Bussinessdata类代码。现在如何解决我的问题 帮帮我!!

3 个答案:

答案 0 :(得分:1)

数组中没有objectAtIndex。你需要做这样的事情:

arrDict[i]

而不是

arrDict.objectAtIndex(i)

修改

正如我们在评论中讨论的那样,这就是你所需要的

if((arrDict[i].name.lowercaseString?.containsString(text.lower‌​caseString)) == true)

答案 1 :(得分:1)

试试这个:

func searchMethod(notification:NSNotification)
    {

    isSearching = true;

    let text:String = notification.userInfo!["text"] as! String;

    arrSearch = [];

    for(var i=0;i<arrDict.count;i++)
    {
        if((arrDict[i].BusinessName.lowercaseString?.containsString(text.lower‌​caseString)) == true)
        {
            arrSearch.addObject(arrDict[i]);// or arrSearch.append(arrDict[i])
        }
    }

    TableViewList.reloadData();

}

答案 2 :(得分:1)

objectAtIndex:属于NSArrayobjectForKey:属于NSDictionary
两者都不适用于Swift原生类型。

但有两个致命问题:

Businessdata是一个自定义类,根本不响应objectForKey:,并且类中没有属性name

假设您正在讨论属性BusinessName,并且逻辑应该过滤所有Businessdata个实例,其BusinessName小写字符串包含您可能编写的搜索字符串

arrSearch = [Businessdata]()

for item in arrDict {
  if let businessName = item.BusinessName as? String where businessName.lowercaseString.containsString(text.lowercaseString) {
    arrSearch.append(item)
  }
}

swifiter

arrSearch = arrDict.filter({ (item) -> Bool in
  if let businessName = item.BusinessName as? String {
    return businessName.lowercaseString.containsString(text.lowercaseString)
  }
  return false
})

请遵守命名约定,并始终使用以小写字母开头的变量名称。