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.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

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 that library. The print method you use to print a message to the console is also part of that library. When you're developing your own Swift apps, you'll often wanna use elements in the Standard Library, rather than reinventing the wheel, but it's a big library, so how do you know what's in there? 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 doubles 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 Bullseye. 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 okay. 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're going to do is convert this from 37 to an actual random number. So how might we do that? We'll check the 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 and you'll see there's a Standard Library section. This tells you all kinds of different things that you can do with some of the built-in data types like Int, 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're going to do is look at Int since we're working with integer numbers here and we want to 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 and see random and let's click on this one. And this goes to a detailed 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 almost exactly what we want right? 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 just put another dot, but we can copy this line of code right here to start us off. So select this code, press command C to copy, go back to our code, and paste that in for the initial value of target. And again, we want a hundred to be possible, so instead of less than, just put an extra dot there. To test it, open up Content View and let's just run it right here in the canvas. Make sure that live mode is on with the play button highlighted in the lower left of the canvas. There was a chance we would still see 37 here, it's just a random number after all but it is 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 or how to do it. 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 forget things. As you progress through our learning path and learn more about iOS development in 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 this course and even start striking off into new things on your own.