Here is the Rosetta Stone for Objective-C to Swift
If you're moving from Objective-C to Swift, or perhaps from Swift to Objective-C, it can be useful to have a quick start guide showing equivalent code samples in both languages. Well, that's exactly what you'll find below: a Rosetta stone of Apple development, demonstrating variables, collections, functions, classes, and more.
In all instances, Objective-C code is shown on the left and the equivalent Swift code on the right. Where necessary I have added small clarification notes to give you more understanding.
If you can already program in Swift and want to get up to speed with Objective-C as quickly as possible, I have just the book for you: Objective-C for Swift Developers.
Create a variable | |
---|---|
|
|
Create a constant | |
Constants are used infrequently in Objective-C. |
Constants are extremely common in Swift. |
Create a variable array | |
Generics in Objective-C are a contentious issue; you will see both styles. |
The first style is strongly preferred. |
Create a constant array | |
|
|
Adding a value type to an array | |
Value types must be wrapped in a reference type before being added to a collection. |
|
Create a dictionary | |
|
|
Define an enum | |
|
|
Appending a string | |
|
|
Adding to a number | |
|
|
String interpolation | |
|
|
Printing debug information | |
|
|
Checking a condition | |
---|---|
|
|
Looping a set number of times | |
|
|
Looping over an array | |
|
|
Switching over a value | |
Many people are unaware that Objective-C has range support, so you might see alternative syntax. |
Swift will not fall through cases unless you use the |
A function that accepts no parameters and returns nothing | |
---|---|
|
|
A function that accepts no parameters and returns a string | |
|
|
A function that accepts a string and returns a string | |
The name for the first parameter should be part of the method name itself. |
|
A function that accepts a string and an integer, and returns a string | |
|
|
Returning multiple values from a function | |
Objective-C does not support tuples, so dictionaries or arrays are used instead. |
|
A closure that accepts no parameters and returns nothing. | |
|
|
A closure that accepts no parameters and returns a string. | |
|
|
A closure that accepts a string parameter and returns a string. | |
|
|
Creating an empty class | |
---|---|
|
It's preferable to use structs rather than classes. You may not need to inherit from |
Creating a class with two properties | |
|
Swift requires you to create an initializer to give these properties default values. |
Creating a class with a private property | |
Objective-C doesn't really support private properties, so this workaround is common. |
|
Creating a class with an instance method | |
|
|
Creating a class with a static method | |
The difference is small: |
Swift also supports static methods – methods that may not be overridden in a subclass. |
Extending a type with a new method | |
|
|
Checking the class of an object | |
|
|
Typecasting | |
|
The former will set |
Running code on different threads | |
---|---|
|
|
Link copied to your pasteboard.