Optional in Swift, return count of array

com.iavian

Help me with Optional hell in Swift. How to return count of array for key "R". self.jsonObj can be null

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
  return (self.jsonObj["R"]! as? NSArray)?.count;
}
vacawama

Let's take this a step at a time.

self.jsonObj may be nil so you need to treat it as an Optional:

self.jsonObj?["R"]

This will either return 1) nil if self.jsonObj is nil or if "R" is not a valid key or if the value associated with "R" is nil 2) an Optional wrapped object of some type. In other words, you have an Optional of some unknown type.

The next step is to find out if it is an NSArray:

(self.jsonObj?["R"] as? NSArray)

This will return an Optional of type NSArray? which could be nil for the above reasons or nil because the object in self.jsonObj was some other object type.

So now that you have an Optional NSArray, unwrap it if you can and call count:

(self.jsonObj?["R"] as? NSArray)?.count

This will call count if there is an NSArray or return nil for the above reasons. In other words, now you have an Int?

Finally you can use the nil coalescing operator to return the value or zero if you have nil at this point:

(self.jsonObj?["R"] as? NSArray)?.count ?? 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift Array optional Type and subscripting (Beta 3)

From Dev

Array initializer shorthand with optional values in Swift

From Dev

What is this called "var count:Int { return items.count }" in Swift?

From Dev

Why is an optional array not enumerable in Swift?

From Dev

Swift optional Array property is immutable?

From Dev

Swift: optional array count

From Dev

archive array of optional structs with NSCoding in Swift?

From Dev

NSCalendar in Swift - init can return nil, but isn't optional

From Dev

array count error swift

From Dev

Optional array vs. empty array in Swift

From Dev

How to count selected UITableView rows in Swift. Optional in indexPathForSelectedRow

From Dev

Swift Optional Array Index Error

From Dev

Swift Optional of Optional

From Dev

Return non-optional with .Success enum in Swift

From Dev

Ruby: return total count of each item in array

From Dev

Swift 2 parse Json as Optional to array

From Dev

Swift 2 count duplicate in array

From Dev

Count elements of nested array swift

From Dev

swift: Elegant Way to Map Optional to Array

From Dev

Count elements of nested array swift

From Dev

Swift optional Array property is immutable?

From Dev

Optional chaining and Array in swift

From Dev

array count error swift

From Dev

Swift return string from Array

From Dev

Swift2: return a optional type object

From Dev

Swift 2 count duplicate in array

From Dev

Swift: Reduce optional array of models to single Bool by optional property

From Dev

Count of Items in an Array within an Array on Swift 3

From Dev

URL array throwing optional error swift