输入' BusinessData'没有会员&sort'singComparator'

时间:2016-03-31 09:35:09

标签: ios swift sorting tableview

我从一个网址获取一些数据,我尝试在表格视图中显示。在那之前,我直接使用所有数组,搜索,排序。它工作得很好。但是现在,当我为我的表视图创建单独的类时,我收到此错误

Type 'BusinessData' has no member 'sortUsingComparator'在我的排序功能中。我给了3个按钮动作。在按钮动作旁边我声明了这个排序代码。但是现在我收到了这个错误。

我的bussinessData.swift

import UIKit

class BusinessData {

    var BusinessName: String?
    var Address: String?
    var Rating: Float?
    var ContactNumber: String?

    init(json: NSDictionary) {
        self.BusinessName = json["business_name"] as? String
        self.Address = json["location"] as? String
        self.Rating = json["__v"] as? Float
        self.ContactNumber = json["phone_no"] as? String


    }


}

我的viewcontroller.swift代码:

import UIKit


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    var isTapped:Bool?   // cell tap checking bool

    var selectedIndex:NSIndexPath?


    @IBOutlet weak var RightMenu: UIView!




    @IBOutlet weak var searchBar: UISearchBar!


    @IBOutlet weak var tableView: UITableView!   // UITable view declaration



    var arrDict = [BusinessData]()   // array to store the value from json

    var isSearching:Bool?

    var arrSearch:NSMutableArray=[]


    override func viewDidLoad() {


        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.jsonParsingFromURL()  // call the json method

        searchBar.returnKeyType = UIReturnKeyType.Done;
        searchBar.hidden = true;


        // nib for custom cell (table view)
        let nib = UINib(nibName:"customCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "cell")

         indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 90, 90))
        indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        indicator.center = self.view.center
        indicator.color = UIColor .redColor()
        self.view.addSubview(indicator)



    }
    override func viewWillAppear(animated: Bool) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "searchMethod:", name: "search", object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "endSearch", name: "endSearch", object: nil);
    }




    // web services method
    func jsonParsingFromURL ()
    {
        let url:NSURL = NSURL(string: "http://sample url/Fes?current_location=toronto&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyIkX18")!

        if let JSONData = NSData(contentsOfURL: url)
        {
            if let json = (try? NSJSONSerialization.JSONObjectWithData(JSONData, options: [])) as? NSDictionary
            {
                if let reposArray = json["data"] as? [NSDictionary]
                {

                    for item in reposArray
                    {
                        let itemObj = item as? Dictionary<String,AnyObject>

                        let b_type = itemObj!["business_type"]?.valueForKey("type")


                        if (b_type as? String == "Taxis")
                        {

                            arrDict.append(BusinessData(json: item))

                        }
                    }
                }

            }
        }

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        if(isSearching == true)
        {
            return arrSearch.count;
        }
        return self.arrDict.count
    }


    // number of rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 1
    }

    // height for each cell
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
    {
        return cellSpacingHeight
    }



    // calling each cell based on tap and users ( premium / non premium )
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {



        let cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell

        if(isSearching == true)
        {
            cell.vendorName.text = arrDict[indexPath.section].BusinessName
            cell.vendorAddress.text = arrDict[indexPath.section].Address
            cell.VendorRating.rating = arrDict[indexPath.section].Rating!
        }
        else
        {
            cell.vendorName.text = arrDict[indexPath.section].BusinessName
            cell.vendorAddress.text = arrDict[indexPath.section].Address
            cell.VendorRating.rating = arrDict[indexPath.section].Rating!
        }
        return cell
    }



    // MARK:
    // MARK: Sort Method

    @IBAction func sortByRevBtnPress(sender: AnyObject)
    {

        self.indicator.startAnimating()
        self.indicator.hidden = false
        RightMenu.hidden = true

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue()){
            self.indicator.stopAnimating()
            self.indicator.hidden = true


        };
        self.tableView.reloadData()


    }


    @IBAction func sortByAZBtnPress(sender: AnyObject)
    {
        RightMenu.hidden = true



        BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in

            if let name1 = dict1["business_name"] as? String, name2 = dict2["business_name"] as? String
            {
                return name1.compare(name2)
            }

            return .OrderedAscending
        }
        self.tableView.reloadData()
    }

    @IBAction func sortByRatingBtnPress(sender: AnyObject)
    {

        RightMenu.hidden = true

        BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in

            if let name1 = dict1["rating"] as? String, name2 = dict2["rating"] as? String
            {
                return name2.compare(name1)
            }

            return .OrderedDescending
        }
        self.tableView.reloadData()


    }


    @IBAction func sortByRecentBtnPress(sender: AnyObject)
    {

        RightMenu.hidden = true

        BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in

            if let name1 = dict1["created_at"] as? String, name2 = dict2["created_at"] as? String
            {
                return name2.compare(name1)
            }

            return .OrderedDescending
        }
        self.tableView.reloadData()
    }

    // MARK:
    // MARK: Search Method

    func endSearch()
    {
        if(isSearching == false)
        {
            isSearching = true;

            searchBar.hidden = false;

            yConstraint.constant = 47;

            self.view.layoutIfNeeded();
        }
        else
        {
            isSearching = false;

            searchBar.hidden = true;

            tableView.reloadData();

            yConstraint.constant = 3;

            self.view.layoutIfNeeded();

            self.view.endEditing(true);
        }

    }


    func searchMethod(notification:NSNotification)
    {
        if(isSearching == true)
        {
            isSearching = false;

            searchBar.hidden = true;

            tableView.reloadData();

            yConstraint.constant = 3;

            self.view.layoutIfNeeded();

            self.view.endEditing(true);
        }
        else
        {
            isSearching = true;

            searchBar.hidden = false;

            yConstraint.constant = 47;

            self.view.layoutIfNeeded();
        }

    }


    // MARK:
    // MARK: SearchBar Method

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
    {

        arrSearch = [];

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

        tableView.reloadData();
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar)
    {
        self.view.endEditing(true);

        isSearching = false;

        searchBar.hidden = true;

        tableView.reloadData();

        yConstraint.constant = 3;

        self.view.layoutIfNeeded();
    }




}

我在这些按钮操作方法中遇到错误:

@IBAction func sortByAZBtnPress(sender: AnyObject)
    {
        RightMenu.hidden = true



        BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in

            if let name1 = dict1["business_name"] as? String, name2 = dict2["business_name"] as? String
            {
                return name1.compare(name2)
            }

            return .OrderedAscending
        }
        self.tableView.reloadData()
    }

    @IBAction func sortByRatingBtnPress(sender: AnyObject)
    {

        RightMenu.hidden = true

        BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in

            if let name1 = dict1["rating"] as? String, name2 = dict2["rating"] as? String
            {
                return name2.compare(name1)
            }

            return .OrderedDescending
        }
        self.tableView.reloadData()


    }


    @IBAction func sortByRecentBtnPress(sender: AnyObject)
    {

        RightMenu.hidden = true

        BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in

            if let name1 = dict1["created_at"] as? String, name2 = dict2["created_at"] as? String
            {
                return name2.compare(name1)
            }

            return .OrderedDescending
        }
        self.tableView.reloadData()
    }

在每种方法中,我都会收到此错误Type 'BusinessData' has no member 'sortUsingComparator' ..在以下行中:

BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in

请帮帮我

由于

1 个答案:

答案 0 :(得分:0)

BusinessData是一个模型类。看起来你真正想做的就是排序var arrDict = [BusinessData]() - 一组BusinessData个实例。

您当前的代码BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in正在尝试在sortUsingComparator模型类上调用类方法BusinessData,并期望获得2个字典。

你想要的更像是:

arrDict.sortUsingComparator { (b1, b2) -> NSComparisonResult in

其中b1b2BusinessData的实例,而不是字典。