TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

What you learned

Swift is technically a C-like language, but as you’ve seen it has a number of features that really make it stand out from the pack. Please don’t worry if you read this chapter and thought, “you know, that just makes no sense” – that’s OK. In fact, it’s perfect normal. Some features, not least optionals, take a good long time to sink in, and you’ll have lots more practice with them as the series progresses.

This was a big chapter with lots of ground covered, so your head might be spinning a bit. If you find something in the list below that you don’t remember, just flick back and re-read. Don’t worry, though: all these things will be covered again in future tutorials, so you’ll have more than enough chance to learn!

  • Xcode’s playgrounds are a great way to test run your code and see immediate results. We don’t use them much in Hacking with Swift because we’re building real projects, but you’ll find them invaluable in your own learning.
  • Swift coders like to make things constant using let where possible, rather than variable using var. Xcode even warns you if you get this wrong.
  • There are lots of types of data, such as strings (“Hello”), integers (5), doubles (5.5555), and booleans (true).
  • You can tell Swift exactly what data type a value holds, but it’s preferable to let Swift figure it out from your code.
  • You can perform arithmetic using +, -, *, and /, and compare with >, <, and ==.
  • String interpolation lets you place values into strings. For example, "Your name is \(name)" will put the value of name directly into the string.
  • Arrays let you group lots of values together, and are type safe – that means you can’t add a number to a string array. You access items in an array by referring to their position, e.g. names[4]. These positions count from zero.
  • Dictionaries also let you group values together, but you get to specify a key rather than just using numbers. For example, person["month"].
  • Conditional statements, e.g. if person == "hater", let you take some action depending on the state of your program. You can combine multiple conditions together using &&.
  • Swift has several types of loops. You saw for i in 1...10, which counts from 1 up to 10 inclusive, for song in songs, which loops every item item in the songs array, and also while true, which causes the loop to go around forever until you exit.
  • You can use continue to stop the current run of the loop and continue from the next item – it effectively jumps back to the top of the loop and carries on.
  • You can use break to exit a loop entirely.
  • Using switch/case blocks is a good way to execute lots of different code depending on a value. You can use ranges, e.g. 2...3, but you must always cover every possible value. You can provide a default case to mean “anything not already matched.”
  • Functions let you write chunks of re-usable code that you can call from elsewhere in your program. They can accept parameters, and can return values.
  • Any data type in Swift can be optional, which means it might not have a value. For example, a variable holding someone’s age would be 18 if that person was 18, or 0 if that person were only a few weeks old. But what if you didn’t know their age? That’s where optionality comes in – you can set the number to nil, meaning “no value yet.” (This is the most confusing thing in Swift, and takes some getting used to.)
  • Swift forces you to unwrap optionals before you use them, to avoid the chance of you accessing non-existent data. This is done using if let.
  • Optionals have a special variety called implicitly unwrapped optionals, which can also have no value but don’t need to be unwrapped before you use them. If you try this and get it wrong, your code will crash – be careful!
  • Optional chaining lets you safely write complex instructions using optionals. For example thingA?.thingB?.thingC = "meh" will do nothing if either thingA, thingB, or thingC are nil.
  • The nil coalescing operator, ??, lets you provide a default value if an optional is empty.
  • Enumerations – or just enums for short – let you declare a new data type that contains a set of specific values, such as rainy, cloudy, and sunny. Swift lets you add extra values to its enums, to track things like “how cloudy is it?”
  • Structs and classes are complex data types: they hold multiple values inside them (called “properties”), and can also have internal functions (called “methods”) that operate on those internal values.
  • Classes are more powerful than structs because one class can inherit from (i.e. build upon) another. However, that adds extra complexity, which isn’t always helpful.
  • You can attach willSet and didSet observers to properties, which means you can ask for code to be run when that property changes.
  • You can make attach access control to properties, which stops other people touching them directly. This is helpful because it forces other developers to use methods to read and write values, which means you can attach extra functionality as needed.
  • If you know you’re working with an object of type A and Swift thinks you’re working with an object of type B, you can use as? typecasting to tell Swift what it really has. You can use as! instead, but again your app will crash if you’re wrong.
  • Closures are a bit like functions that you can place inside variables and pass around. They capture any values that they need to work.

Phew! That’s a lot, but again: all those things will be covered again in more detail in upcoming tutorials – this was just a primer.

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.