我想成爲顯示對象的一部分,國家的一部分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