Reputation: 3850
The following controller has numberOfRowsInSection: ofUITableViewDataSource.
class CityList: UIViewController,UITableViewDataSource,UITableViewDelegate {
weak var cityListArray:NSMutableArray? = nil
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.cityListArray?.count
}
}
return self.cityListArray?.count
shows Value of optional type int? not unwrapped did you mean to use '!' or "?"
How can I use ternary operator here, to return 0 if the array was empty?
Objective-C had default value returned 0, if the object is nil.
Upvotes: 0
Views: 656
Reputation: 3850
There are two answers for this. One is already mentioned in the comments of the question.
return self.cityListArray?.count ?? 0
Which seems to be a shorter than the one which uses a ternary operator, mentioned below.
return (self.cityListArray != nil) ? self.cityListArray!.count : 0
Upvotes: 1