In this module, we’ll cover some basic, core ideas about Swift.
In this module, we’ll cover some basic, core ideas about Swift. Swift is a type-safe language, which means it has a structure enforced on you, the developer, to make sure your code doesn’t crash and is optimized for speed. You’ll learn what type safety means and how it will affect your own coding. You’ll also learn about code readability and how it’s important for you to be clear with your code’s intent. To help with this, you’ll learn how to add your own notes and comments to your code so you can communicate clearly with others who might, in the future, work on your code base.
You’ll also learn about print(), a critical tool for developers and an important first technique that you’ll use quite a bit in Swift. Print() will show you what’s going on in your code and, when you use it properly, how it will help you debug. You’ll also learn how to use the built-in documentation, which is a critical skill so you can understand the details of Swift.
Learning Objectives
- Understand type safety
- Identify code readability and commenting
- Recognize how the print() method can help debug your code
Skills you’ll gain
DebuggingFunction ModulePrint ModuleProgramming ConceptsSwift (Programming Language)What You'll Learn
- Understand what type safety means in Swift and how it affects your coding
- Identify code readability practices and how to add comments to your code
- Recognize how the print() method helps debug and reveal what your code is doing
- Communicate your code's intent clearly through notes and comments for future collaborators
- Use Swift's built-in documentation to understand the details of the language
Key Takeaways
- Swift is a type-safe language, meaning it enforces structure on the developer to help prevent crashes and keep code optimized for speed.
- Code readability matters because it lets you communicate your code's intent clearly to others who may work on the code base in the future.
- Adding your own notes and comments to your code is a way to communicate clearly with future collaborators.
- The print() method is a critical tool that shows what is going on in your code and, used properly, helps you debug.
- Knowing how to use Swift's built-in documentation is a critical skill for understanding the details of the language.
Frequently Asked Questions
What does this Swift module cover?
This module covers basic, core ideas about Swift, including type safety, code readability and commenting, the print() method for debugging, and how to use Swift's built-in documentation. Its lessons are Type Safety, Code Commenting, Printing Values, Code Readability, and Documentation.
What does it mean that Swift is type-safe?
Swift is a type-safe language, which means it has a structure enforced on you, the developer, to make sure your code doesn't crash and is optimized for speed. In this module you'll learn what type safety means and how it will affect your own coding.
How does print() help in this course?
You'll learn about print(), described as a critical tool for developers and an important first technique you'll use quite a bit in Swift. It shows you what's going on in your code and, when used properly, helps you debug.
What skills will I gain from this module?
The module covers skills including Debugging, Function Module, Print Module, Programming Concepts, and Swift (Programming Language).
Is this module suitable for beginners?
Yes. As a Swift Beginner module, it covers basic, core ideas about Swift such as type safety, code readability and commenting, and using print() to debug your code.
Transcript
Show transcript (free preview lesson)
Transcript of the free preview lesson. Remaining lessons unlock with the full course.
Hello, my name is Mark Nair. And in these lessons, you'll learn basic Swift terms and concepts, including type safety, code commenting printing values, code readability, and documentation. Swift is a type safe language. What that means is we have different data types that we use in the language and Swift really enforces the safety, the structure of those types. Swift will ask you to clarify the intent of what you're doing. That way you can't misuse or inappropriately put a different type into one of your containers that you're using or variables or constants in your code. When you mix up types it gets very confusing and very difficult to debug. There are other programming languages that are not type safe and people deal with it just fine. But Swift takes a different approach. The approach is focus on your code safety trying to avoid crashes, or if something does come up warning you ahead of time so that you are able to take care of it before it gets too far down the road and chaos goes crazy. So, let's take a look at what this means. I'll put in a few lines of code here. Do not worry about what these lines, the syntax means. We're not to that point yet. Just concern yourself with what the type and the safety-ness of the type is. And I'll explain it as we go on. So for example, I'll set up my favorite number is of type int, which is 42. This just means that int a whole number and this is the value. So, the way I read this is this that I'm setting up, this is a constant, we'll talk about that later on, is of type int, which means the container. What this is holding is an integer and it cannot be anything, but in integer, another thing let my lunch is of type string and that's equal to let's just say chicken curry. Now the value of my lunch will always be a string value and that will be this, whatever the string is it doesn't have to be chicken curry all the time. That would be just boring lunches but it is a value of string. Something in quotation marks that is from Neptune is of type bool. And I'll just say that's false. A bool or a Boolean value is a true or false value. So, is from Neptune is false, right now all of this is perfectly fine. If I run it, everything works great. No errors, no warnings. Let me stretch this out over here. We see the results. Now, what I cannot do is pass another type into one of these types. So 42, for example, here what I can't say is my favorite number is 7, 6, 3, 4, dot 63. That will not work because this is no longer a whole number. This is what's called a double, not an integer or an int. Likewise, I can't say 42 because now this is a string that I'm assigning to it but I've told Swift my favorite number is of type int. So, Swift is complaining. Can't convert string to specified int. So, it has to be the type that I've initially set up. Now, 42 could be it, or it could be this. It doesn't matter as long as it's a whole number or an in integer. It could be that. Still the same type. Now... One of the things that's very Swifty is we simplify our code. We do not have to specify this business here. We don't have to say of int. We can get rid of it and Swift will understand what we're trying to do. So, here I'll get rid of this. And here I'll get rid of this. This is called type inference. Swift is inferring what our intent is by the value that we've given things. We're not getting anything complaining, nothing else is happening, but to double check I can hold the option key down on my keyboard and hold it over these assignments here. And then if I click that, the popup says let my favorite number of type int. I can see it right here, even though it doesn't show it to me in the code. Swift is doing a pretty good job here of understanding what I'm trying to do. So, my lunch should be a string and that's good. So, let's try this from scratch. Just so you believe me, let another number is equal to 63 point 9, 7, 6. Now, if I hold the option key down a question mark pops up click this, let another number is of type double. And that's exactly what I expect. Type safety is very important on the security of our code but it's very, very important to help you fix problems. At the beginning, you might feel a little shackled especially if you're coming from a language like Python that has a more dynamic type way of looking at the world. And Swift is very specific, but as you build bigger things and more complex things, you'll come back, being thankful that Swift is a tight, safe language. It's constraining you to make sure that you follow those rules the guidance that you've given early on. Thanks for watching. Stay tuned for the next lesson where I'll show you how code commenting works.
Learn on the Go
Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.