3 ms·
In swift you do mymethod?.call(...) Where method can be nil and it has the same effect. Similarly object?.something has the same effect. Use exclamation mark in
by Cloudef 4y ago
In swift you do mymethod?.call(...)
Where method can be nil and it has the same effect.
Similarly object?.something has the same effect.
Use exclamation mark instead of question mark, to make swift runtime panic if the object or method is nil. Obj-c is objectively worse in every way and easy way to improve bugs in your code that go unnoticed for long time.
Another cool thing is that you can use ?? At end of expression that can return nil to decide what to do if nil is returned (for example return something else). So swift actually reduces the amount of ifs required.
- mojuba 4y ago> In swift you do mymethod?.call(...) Where method can be nil and it has the same effect. No, it doesn't. (You probably meant object?.mymethod()). If mymethod returns int, then in Swift the result of this expression is strictly Optional<Int>, whereas in Objective-C it's Int defaulting to zero if object is nil. The implications in terms of safety are significant.
- Cloudef 4y agoI did mention object?.mymethod as well, method?.call is needed if you want to call on nullable method itself in a safe way.
- setr 4y agoDefaultZero = Object?.mymethod() ?? 0; Optional-null + null-coalescing is strictly more flexible than obj-c’s behavior, while more sanely defined, with very minimal syntax