Ternary Unwrapping in swift

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Ternary Unwrapping in swift



In swift i'm using this code:


var categories: Results<Category>? //Realm dataType

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

if let categories = categories, !categories.isEmpty
return categories.count
else
return 1




I'm now looking to structure the code for my tableView as a ternary operator but don't know quite how to do this. I found the following page:
https://dev.to/danielinoa_/ternary-unwrapping-in-swift-903 but it's still unclear to me.



What i tried is:


return categories?.isEmpty ?? ($0).count | 1



or


let result = categories?.isEmpty ?? ($0).count | 1
return result



but both giving errors. Any idea how I can solve this?





what you want to achieve ?
– vivekDas
Aug 12 at 9:13





just shorten my code :)
– Bjorn Morrhaye
Aug 12 at 9:32





Why return 1 if the array is nil? Shouldn't it be 0?
– Joakim Danielson
Aug 12 at 9:43





I always want to return a row in my tableView. If my array doesn't contain any items, the cell functions like a placeholder. The standard value of the cell then is "please add items"
– Bjorn Morrhaye
Aug 12 at 9:49




3 Answers
3



Instead of ternary you could use Optional.map to make it even simpler:


Optional.map


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return categories.map $0.count ?? 1



Or mixing Optional.map and ternary to achieve what you I think are trying to achieve:


Optional.map


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return categories.map !$0.isEmpty ? $0.count : 1 ?? 1





Thanks for this. ! i didn't know about "map". Could you explain a bit because i'm a bit confused. categories.map unwraps the optional, then i check if the unwrapped value is not empty. If not empty i return the count of the value, if it is empty, i return 1, but what does the last part do ? from ?? 1
– Bjorn Morrhaye
Aug 12 at 9:47





Your categories are Optional, so it could have some value or not. There are three cases to check in this case: 1. categories are nil, 2. categories are not nil but isEmpty = true, 3. categories are not nil and isEmpty = false. The last part you are asking about is to make sure to display the proper value in case number 1.
– Michał Kwiecień
Aug 12 at 9:51


categories


categories


categories


isEmpty = true


categories


isEmpty = false





i see, perfect ! thanks for the solution and explanation ! much appreciated !
– Bjorn Morrhaye
Aug 12 at 9:56



Return 1 for nil and empty array , otherwise the value of count


count


return (categories?.count ?? 1) + (categories?.isEmpty ?? false ? 1 : 0)


return categories?.count != 0 ? ($0).count : 1





i get following error: Type of expression is ambiguous without more context
– Bjorn Morrhaye
Aug 12 at 9:41







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard