Swift For Dummies (2015)
Part V. The Part of Tens
Chapter 22. Ten Objective-C Features That Aren’t in Swift
If you are an experienced Objective-C developer, this chapter reminds you of some features you may be used to that aren’t available in Swift. In each case, I show you workarounds and strategies for replacing your old tried-and-true Objective-C friends. And don't worry: In most cases, you'll end up writing less code and your code will be more robust.
Saying Goodbye to Header (.h) Files
In Swift, the header (.h) files are gone. The .h and .m files for headers and bodies (originally called messages) of a class are consolidated into a single .swift file.
Saying Farewell to Dangling Pointers (Almost Always)
It's hard (but not impossible) to reference a dangling pointer in Swift. Over the last few years, Objective-C has also made such references more difficult, but they remain distinctly possible. You can avoid issues with these references if you make certain that pointers to instances are always set before they are used, and that they’re set to nil before the instance is deallocated. Swift uses very few pointers, so they can't dangle.
Forgetting About Uninitialized Variables and Properties
Swift requires initialization of declared properties, variables, and constants. This immediately removes a whole set of problems that can occur when you reference uninitialized variables and may either crash or cause unexpected (and frequently unrepeatable) errors.
Exploiting a Common Superclass Like NSObject
Most objects in Objective-C are subclasses of NSObject; almost all of them conform to the NSObject protocol. Thus, collections such as NSArray and NSDictionary can contain objects of any kind as long as they are subclasses of NSObject. In Swift, the elements of an array or dictionary do not have a common superclass, but they do have to have a common type so that they can be manipulated. This is now your responsibility.
Managing Type Casting
Objective-C manages type casting where it can, but if your assumption about the correct cast differs from Objective-C’s, your code may crash. With Swift, you have to do the casting explicitly. However, Swift can infer a type from initial values of variables or constants, so, on balance, your code is more robust and you may actually need less of it because you can let Swift infer types for you.
Preferring Closures to Blocks
Blocks in Objective-C are like closures in other languages. In Swift, that function is provided by closures, and the documentation reflects this use.
Getting Rid of Legacy Memory Management
Although manual memory management with alloc and dealloc has been deprecated in Swift, much code still exists that either uses it or refers to it in comments and commented-out lines of code. If you are converting old code rather than writing new Swift code from scratch, it's worthwhile to remove these vestiges of manual memory management. It's very unlikely they will return.
Replacing Property Decorators
Instead of Objective-C’s property decorators, Swift has annotations in declarations. The mixture of attributes, such as readonly, strong, weak, and the like, is gone with Swift. The necessary information is encompassed in types.
Using Swift Style to Access Class Properties
Swift style uses dot syntax to reference objects within a class, structure, or enumeration. Remove any left-over bracket-based syntax.
Clarifying Swift Access Control
The privacy directives in Objective-C are handled differently in Swift. In Objective-C you use the following directives:
Declarations of methods and properties can also be placed both in the .h file or in a class extension of a .m file. This provides two disjoint ways of specifying visibility.
Swift has no .h or .m files and therefore no class extensions with hidden declarations. It has a single access control model using
These are part of the language itself rather than being compiler directives.
The Swift access control structure is described in Chapter 14.