Your First iOS & SwiftUI App: An App from Scratch
Jan 11 2022 · Swift 5.5, iOS 15, Xcode 13
Part 3: Coding in Swift
21. The Swift Standard Library
Leave a rating/review
The Swift language comes with many pre-built data types and functions you can use in your apps to solve a variety of common tasks, called the Swift Standard Library.
You can think of the Swift Standard Library as a big box of books filled with code that you can use in your apps.
In fact, you’ve been using many elements from the Swift Standard Library already. The data types Int, Double, String, and Bool are all part of the Swift Standard Library. The print method you used to print a message to the console is also a part of the Swift Standard Library.
When you’re developing your own Swift apps, you’ll frequently want to use elements in the Swift Standard Library, rather than re-inventing the wheel. But it’s a big library, so how can you know what’s available?
Luckily, Xcode comes to the rescue. Xcode comes with detailed developer documentation of every single class and method that you can use in iOS, including everything in the Swift Standard Library.
You’ve already used the Xcode documentation once - back when we researched how to round a Double value to the nearest Integer, using Double’s
In this exercise, I’ll show you how you can use the Xcode developer documentation to take a tour of what’s available in the Swift Standard library.
Then, we’ll use it to look up how we can get a random number, which you’ll need to generate the random target for the user to aim for in Bull’s Eye.
Note that the Swift Standard Library and its documentation may seem a bit overwhelming at first, and some of the explanations and syntax might not make total sense to you yet.
That’s OK - for now, the important thing is that you just understand that the documentation is there and the general process of using it.
Help \ Developer Documentation
Show Swift Standard Library section, give brief tour
Dig into Int, show the random. Click through to get full details, and show example.
Update the target property in Game.swift:
var target: Int = Int.random(in: 1...100)
Build and run a few times to show it’s indeed a random number.
Remember when you’re learning iOS development, you don’t need to memorize every single thing you can do. Instead, you just need to focus on learning the general concepts, and what’s possible - and then you can refer to the documentation when you inevitably forget things.
As you progress through our learning path and learn more about iOS development and Swift, the documentation will start to make a lot more sense, and you’ll eventually get to the point where you can use it to refresh your memory on what you’ve learned in the course, and even start striking off into new things on your own.