Getting started with Swift
I, like many others, was drawn to software engineering by the prospect of building iOS apps. I recently completed Flatiron School’s immersive web development program where we covered the fundamentals of programming and web technologies. I am hoping to piggyback off what the program taught me by learning Swift and iOS development.
Let’s jump into it.
Xcode
Xcode is a text editor created by Apple specifically for iOS development. The text editor comes with “batteries included” featuring snippets, live preview and a lot useful tools. You can tell that Apple really invested in the developer experience.
Objective-C → Swift
iOS apps were originally built in a programming language created by Apple called Objective-C. In 2014, they released Swift, a “modern” programming language for iOS development.
Swift
Swift can be written for phones, desktops, servers or anything that runs code. “It’s a safe, fast, and interactive programming language that combines the best in modern language thinking with wisdom from the wider Apple engineering culture and the diverse contributions from its open-source community. The compiler is optimized for performance and the language is optimized for development, without compromising on either.”
Some of the modern programming patterns that Swift adopted are:
- Variables are always initialized before use.
- Array indices are checked for out-of-bounds errors.
- Integers are checked for overflow.
- Optionals ensure that
nil
values are handled explicitly. - Memory is managed automatically.
- Error handling allows controlled recovery from unexpected failures.
“Swift combines powerful type inference and pattern matching with a modern, lightweight syntax, allowing complex ideas to be expressed in a clear and concise manner. As a result, code is not just easier to write, but easier to read and maintain as well.”
Variables
There are two types of variables in Swift:
Standard Variable:
var age: Int = 24
Constant Variable:
let name: String = "Tyler"
Values are never implicitly converted to another type. If you need to convert a value to a different type, explicitly make an instance of the desired type.
let label = “The width is “
let width = 94
let widthLabel = label + String(width)
Interpolation:
print("\(name) is \(age) years old.")
Dictionaries:
Something I have not seen before in my web dev journey, Dictionaries are a great way to store key/value pairs that you can iterate over.
var occupations = [ “Malcolm”: “Captain”, “Kaylee”: “Mechanic”, ]occupations[“Jayne”] = “Public Relations”