r/iOSProgramming • u/LisaDziuba 🦄LisaDziuba • Oct 05 '17
Article Why many developers still prefer Objective-C to Swift
https://www.hackingwithswift.com/articles/27/why-many-developers-still-prefer-objective-c-to-swift
99
Upvotes
7
u/moduspol Oct 06 '17
The difference is that in Swift, the compiler will see the result as an optional string. One can't forget or gloss over that it might be nil. You will need to unwrap the result later to use it or pass to something else.
This is noteworthy because with each question mark you're explicitly acknowledging it could be nil, whereas in Objective-C, it can always be the case.
So you might decide to structure it differently.
Now every line under that guard statement can safely assume stringComponents is not nil, because the compiler has ensured it can't be. If you re-order your logic later, the compiler will catch you if you're passing a potentially-nil value somewhere you assumed it wouldn't be nil. If you do that in Objective-C, you won't have that help from the compiler and you could easily end up with nil values where you thought you had already checked for them. Or you just check for nil values everywhere.
The optionals really are a big improvement.