我从一个网址获取一些数据,我尝试在表格视图中显示。在此之前,我直接使用了所有array,search,sorting.It效果很好。但是现在,当我为表格视图创建单独的类时,出现此错误
我的排序功能中的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
请帮帮我
谢谢
最佳答案
BusinessData
是模型类。看起来您真正想要做的是对var arrDict = [BusinessData]()
进行排序-一个BusinessData
实例的数组。
您当前的代码BusinessData.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in
试图在sortUsingComparator
模型类上调用类方法BusinessData
,并且期望返回2个字典。
您想要的更像是:
arrDict.sortUsingComparator { (b1, b2) -> NSComparisonResult in
其中
b1
和b2
是BusinessData
的实例,而不是字典。关于ios - 类型“BusinessData”没有成员“sortUsingComparator”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36329562/