Nil Coalescing Operator Swift

Nikita Zernov

I've got one problem in swift. In Objective-C I write like this:

return !item ? self.people.count : item.children.count

So, if item is nil it returns self.people.count, otherwise it returns item.children.count. How to write such thing in Swift?

return true
return (item as? Person)?.children.count ?? self.people.count

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift nil coalescing operator with array

Nil coalescing operator for dictionary

Coalescing Swift operator for an empty non-nil string

Swift 4.2, Xcode 10.2 nil coalescing operator warning

swift nil coalescing with doubles

Nil Coalescing Operator with mixed types

Why is the nil coalescing operator ?? returning nil?

Why doesn't Swift nil-coalescing ternary operator return unwrapped type?

Compiler not understanding Nil Coalescing Operator on NSTimeInterval

Nil-Coalescing Operator without changing value

Why is Swift nil-coalescing returning an Optional?

Null-coalescing assignment operator in Swift 3

Using nil-coalescing operator with try? for function that throws and returns optional

What is the role of the nil-coalescing operator "??" in optional chaining?

Why does the nil coalescing operator wrap an implicitly unwrapped default value?

Is Nil-Coalescing always needed when using firstIndex(of: Character) in Swift?

Swift: compile-time error with optional chaining and nil-coalescing

Left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used

Type inference fails when using nil-coalescing operator with two optionals

Maybe coalescing operator

Opposite of nullish coalescing operator

Is there a "null coalescing" operator in JavaScript?

Nullish Coalescing operator in JavaScript

Null coalescing operator (??) with return

PHP - null coalescing operator

Coalescing operator in linq query

Null coalescing operator override

Use of coalescing operator in Typescript

When in swift 3.0 nil coalescing used, didn't get expected result