我想成为显示对象的一部分,国家的一部分countryID
,这与对象一致。 在不同的城市,有对象 我想表明,与Idi的城市等于countryID
。在tableview中过滤数据
for key in self.cityId{
if key == self.countryID{
self.countCity++
}
}
但是当我运行控制台回报
和模拟器
代码:
写numberOfRowsInSection
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
for key in self.cityId{
if key == self.countryID{
self.countCity++
}
}
return countCity
}
和的cellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cityCell", forIndexPath: indexPath)
let city = cityArray[indexPath.row]
if city.cityId == countryID{
if let NameCity:String = city.nameCity {
cell.textLabel?.text = NameCity
print(NameCity)
}
}
// Configure the cell...
return cell
}
解析API
func GetPassCity(){
NSURLSession.sharedSession().dataTaskWithURL(urlCity){[unowned self] (data ,response ,error) in
if error != nil{
print("A")
print(error!)
}else{
do{
//readin data from Server
let posts = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]]
//save data
for post in posts{
var postCity:City?
if let idCity = post["userId"] as? Int , let nameCity = post["title"] as? String{
postCity = City(idCity: idCity, nameCity: nameCity)
self.cityId.append(idCity)
}
self.cityArray.append(postCity!)
dispatch_async(dispatch_get_main_queue()){
print(self.countCity)
self.tableView.reloadData()
}
}
}catch let error as NSError{
print("B")
print(error)
}
}
}.resume()
print(cityArray)
}
你认为是什么问题?
'self.countCity + = 1'或之前,我已经这样做了'self.countCity ++' – boidkan
。但它是错误'预期的表达式后运算符' – phantom
然后尝试解包countCity因为'self.countCity!++'只是确保它被初始化,而不是零 – boidkan