Try using the nil coalescing statement .
According to Apple documentation:
The nil coalescing (a? B) statement expands optional a if it contains a value, or returns the default value b if a is nil.
So your function might look like this:
func numberOfObjectsInMyArray() -> Int { return (myArray?.count ?? 0) }
I agree with others that this may be a bad idea for a number of reasons (for example, it looks like there is an array with the number “0” when there really is no array), but even bad ideas need to be implemented.
EDIT:
So, I am adding this because two minutes after I posted this answer, I came across a reason to do what the author wants to do.
I am implementing the NSOutlineViewDataSource protocol in Swift. One of the features required by the protocol:
optional func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int
This function requires the return of the number of children from the item parameter. In my code, if an element has any children, they will be stored in an array, var children: [Person]?
I will not initialize this array until I add it to the array.
In other words, while I am providing NSOutlineView data, children may be nil , or it may be filled, or it may be once filled, but subsequently all objects have been removed from it, in which case it will not be nil , but it count will be 0 . NSOutlineView doesn't care if children - nil - all he wants to know is how many lines it takes to display item children .
So, in this situation, it makes sense to return 0 if children is nil . The only reason to call the function is to determine the number of NSOutlineView lines. It doesn't matter if the answer is 0 , because children is zero or because it's empty.
return (children?.count ?? 0) will do what I need. If children is nil , it will return 0 . Otherwise, it will return count . Excellent!