Your First iOS & SwiftUI App: An App from Scratch

Feb 13 2023 · Swift 5.7, iOS 16, Xcode 14

Part 3: Coding in Swift

21. The Swift Standard Library

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 20. Introduction Next episode: 22. Intro to Unit Testing

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 21. The Swift Standard Library

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 rounded() method.

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.

If you open up Game.swift, you’ll see that right now we’ve hard-coded the random target to 37. And, in this episode what we wanna do is convert this from 37 to an actual random number.

So, how do we do this? Let’s check developer documentation.

Just like you can access the Human Interface Guidelines, you can find Developer Documentation from the Help menu.

Over here on the left, under App Frameworks, expand Swift, you’ll see there’s a standard library section.

This tells you all kinds of different things you can do with some of the built-in data types like ints, doubles, and strings.

We’ve worked with all three of those so far.

There’s also arrays and dictionaries which we’ll work with later on.

And, there’s a big section here on the Swift Standard Library that has all kinds of useful information about things you can do with numbers, basic values, strings, collections of data and so on.

So, what we wanna do is we wanna look at Int since we’re working with integer numbers here and we wanna just scroll through here and see what kind of things we can do.

So, it tells us how to convert floating point values. We actually did that. We used this method here to convert a double into an integer.

Also converting strings, we’ve done some of that!

And, we scroll down just a little bit here. We see, Random.

And let’s click this first one here. And, this goes to a detail page that explains how to use this method.

If you scroll down some of these pages actually have example code that shows you how to use them. And, it’s funny cause this is basically exactly what we want.

We want a random number between one and a hundred. So, what this syntax means is one up to but not including a hundred. So, that’s what the less than symbol means.

This would actually be giving us a number between one and 99. So, it’s almost what we want. We want between one and a hundred. And, if you want 100 to be included instead of a less than sign, you put another dot there.

But, we can copy this line of code right here to start us off.

So, select this code, and press Command-C to copy. Go back to our code. And paste that in for the initial value of target.

And again, we want the a hundred to be possible. So, instead of a less than one we put a dot there instead.

var target: Int = Int.random(in: 1...100)

To test it, open up ContentView, and let’s run it just right here in the canvas. Make sure the live mode is one with the play button highlighted in the lower left of the canvas.

There was a chance we’d still see 37. It’s just a random number after all. But it’s not!

To test it again, I’ll press Option-Command-P to reload the preview.

And you should see a different number again. You can just keep reloading that way, with Option-Command-P, and you should almost-always see a different number.

So, we’re getting random numbers now! Excellent.

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.