Check out a free preview of the full iOS App Development with Swift course:
The "Basic Expressions" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates writing basic Swift expressions in an Xcode playground, including booleans, C-syntax, variables, data types, and valid identifiers. Everything declared in Swift is accessible to every other file in the project.

Get Unlimited Access Now

Transcript from the "Basic Expressions" Lesson

[00:00:00]
>> Okay, so it's time to stop talking and seeing the slides, it's time to see some Swift code. So I'm getting out of the presentation. And if you downloaded the assets, remember the assets are in the companion website, in the introduction section. You have there the workshop assets, remember, it's here, workshop assets.

[00:00:26] Inside that folder you are going to find a file with the name playground. Let me show you, Swift101.playground, this one, Swift101.playground, okay? So if you have already installed Xcode, you can just double click that file and it will open in Xcode. So what's a playground? A playground is some kind of special project type available in Xcode that you can also share with an iPad.

[00:01:05] There is an iPad application known as Swift playgrounds. Originally a playground was a way to play with Swift, without actually making a real app, okay? The platform actually evolved a lot over the years, and now you can actually create games using playgrounds without actually creating a real Xcode project.

[00:01:29] And you can also create that directly on the iPad. And you can publish that in the App Store. You're not going to use the playground more than to play here quickly with Swift code, okay? Just for playing with Swift code without actually creating an iPhone application, okay? So we can first focused in the language, directly.

[00:01:51] So you're going to see here, okay? At the left, a couple of some kind of chapters. So the playground today supports, it's like an interactive book, okay? So we can have chapters and pages, it's actually pretty simple, anyway. So you can double click on those chapters and you will see in the main editor, the code that we have right now.

[00:02:18] So that's what I'm doing right now. So Swift code, as I mentioned before, looks like any modern language. For example, let's talk a little bit about expressions. So, what you can see here that we can create variables, very simple. What I'm going to do here is, you don't need to do this, okay?

[00:02:41] Just pay attention for a second. I'm going to create a new playground page just to start from scratch, empty page, okay? I have an empty page. So how would you create the variable? var, see we don't do JavaScript or let's say classic JavaScript, if you will. So var, my name is Maximiliano, okay?

[00:03:06] So what about semicolon? It's there, but typically we don't use it unless we have two sentences or two instructions in the same line. That typically we don't have, okay? So we are not going to use semicolon. So the best practice is not to use it. It's there but we don't need it, okay?

[00:03:26] So if you wanna send something to the console, we have the print function. So it's print and the name. So if you look here, where is the type? Hey, Max, you lied to us. Well, that's how implicit typing or implicit data type works. Actually name has a type.

[00:03:49] So if you press the option key, that's out on some keyboards. So the option key, you can hover references, and the cursor becomes a question mark. So if you option click on any reference, here you will see, for example, that the variable, it's actually a String. So it has the data type, okay?

[00:04:17] Even if I didn't set the data type. And as you can see, I don't have a function, I don't have a class, I'm just writing code. Swift let's you do that. Just writing code. So of course, we can create functions and so on. But, by the way, let's create a function quickly.

[00:04:36] So if you wanna create a function, it's the func keyword, instead of function, func, then the name parentheses, as in JavaScript or many other languages. Code block, so we're still using curly braces for code blocks, and then the code name is a global variable. So it seems like we can actually use it here.

[00:04:55] And if you wanna call the function, it's just greet parentheses. So actually it's familiar to all of you, this code. It's not actually a big deal. So that's why they're going to really fast forward to the differences, and not the part where you're going to feel at home.

[00:05:13] So opening now basic expressions. So let's see just the difference, okay? First, everything that we declare is actually accessible by other files in the same app or framework. We don't know what the framework is, yet. It's like a new term here. A framework is a library, a static library code, like a DLL or Windows, okay?

[00:05:39] When you create just a file that contains code that is not actually an app. So that means that everything that we declare in a file, it's actually accessible by default to other files in the project, okay? We will see later how we can narrow the scope or the visibility of things that we write.

[00:06:03] Boolean conditions don't need parentheses. That looks weird, but it's part of the this idea of reduce redundancy and also reduce bureaucracy. So in most of the other C based languages or when the syntax is C based, we have parentheses here. Well, we don't need the parentheses. If we add the parentheses, nothing happens because it's just the parentheses, but you need to get used to not use them, and that makes your code easier to read, okay?

[00:06:41] I know it's weird, you will see a couple of weird things, but believe me because I was first a JavaScript developer before being a Swift developer, you get used to that, okay? And then you like it. So the same happens here on the while, for example, we don't need parentheses for the Boolean condition.

[00:07:00] By the way we don't have the ++ operator, okay? It's not available, if you try to do that you will get an error. And also you have help here. Did you mean +=1, okay? There is an explanation why ++ is not here. It doesn't matter for now, but it's an executive order.

[00:07:22] We don't have ++, yeah?
>> Thanks, so on line 13, what if I have &&'s and the OR's?
>> [CROSSTALK]
>> If you have an & you can actually do this without parentheses. If you need parentheses it's fine, you can still, because sometimes you say, okay, or wherever well it may be now we have some issues, so maybe we wanna use parentheses at some point.

[00:07:47] It's okay but you don't need this global parentheses to the whole condition.
>> Thank you.
>> Yeah, so Boolean operators are the same, equal, greater than, less than. So that's common. We're not going to spend too much time with that. Name and style guidelines, you will also feel at home if you're a web developer, a full stack developer because this looks like JavaScript or the Java coding style guidelines.

[00:08:16] So variables, constants, and function names use camelCase. So we start with lowercase and then we go uppercase on next words. And data types, we don't know all these data types what they mean. You can guess some of them, but they use TitleCase. So we start with uppercase. So it's actually similar to Java or JavaScript, this is how typically we do this in JavaScript.

[00:08:42] Identifiers, it's similar to most languages such as you cannot use spaces, but you can use emojis, okay? Or you can use special accents. Or for example, Japanese or Chinese characters, okay? Yeah, I'm not sure if you wanna do that, even if you are a Java Japanese developer, okay?

[00:09:04] In terms of sharing the code, but it's possible and you can have a variable as you can see here, that is just a cow, okay? And then this is actually, I mean, it feels funny, but actually if you are a teaching code to kids, you can then say, cow plus cow, or something like that I have two cows.

[00:09:26] It works from that sort of point of view. Of course we are not going to use that, okay? In a real iOS project, but it's actually possible, okay? Have in mind that. So abstain using emojis, because the emoji, another thing that has is that, for example, if I have an error or something, let's say I create a debug variable, I can attach to that variable an emoji.

[00:09:55] So for example a warning emoji. And whatever I'm doing here, that emoji has color, okay? Which highlights our view and we are looking at the code. That may mean I need to delete that before submitting this to the App Store, before going in production, okay? So it might mean something that I need to delete.

[00:10:18] So it's something that I've seen like a pattern, okay? And talking about visibility, scope, visibility, everything is public by default. So it's public to the whole project. We also have private that actually it depends on where this variable is defined. It changes the scope. Because it can be a variable inside a class, inside a structure that we don't know what that is yet.

[00:10:44] And it's going to be private to that particular data type. And also we have fileprivate, which is kind of new to most of you. Fileprivate actually makes that variable available to all the code in this file, even if we have several declaration. For example, I can create two classes in the same file in Swift, something that is not possible on some other languages.

[00:11:12] Well actually I can create a variable here, for example, I don't know, name = "I'm A". And then let's say that class B has a function, let's say test. And that function will create an object of class A. How would you create a variable of class A? Well, my object equals to and you're expecting new A, right?

[00:11:41] Well, good news or bad news, I don't know yet. You pick the 1, we don't have the new keyword. Is the same, we call the, let's say for now, constructor, but without the new keyword. We just call the class name, okay? Let's call this ClassA and ClassB, so we know what we're doing.

[00:12:04] There is no new keyword. So, of course, I can get the name. But if that name is private, then because ClassB is a different class, we don't have the name, okay? You know the deal. But fileprivate will actually make it private only to this file, not to other file.

[00:12:26] But because this ClassB is in the same file, okay? I'm talking about the file system, the file in the file system, actually I can use it. It's something new compared with other languages, okay? But to be honest, more than defining things as private when you want to encapsulate some data that you want to access from the outside, we're not going to focus too much on visibility.

[00:12:55] We don't typically see a lot of Swift code defining public private, fileprivate, it's just the default, most of the time, yeah?
>> So even if you did define it as private, isn't it still a good practice to underscore that invisible?
>> No, in Swift, we don't underscore as a style guideline.

[00:13:15] Of course, if you want, you can do that, okay? The underscore is a valid identifier for your variables. But we don't use that guideline in Swift. You can go with Swift guidelines, and you will find the guideline from different companies on how to name. Of course Apple has its own style guideline, but other companies are also sharing.

[00:13:37] I think, Google has also one for Swift. So they are internal guidelines, they're open source, or they're creating commons. So you can actually take those guidelines, as well, if you want to, where we typically don't use the underscore pattern for private properties, or variables, okay? So the thing is that because this is a compile language, okay?

[00:14:00] The compiler will not compile our app, if you are accessing something private from the outside. So I mean, there is no need to learn other languages, because you can always actually access private variables, if you want. The underscore is like a mark, like a flag, saying from the outside if there is an underscore you shouldn't be doing this.

[00:14:21] But, yeah, we don't use that technique here in Swift, okay? So as you can see for now, okay? It looks familiar, it doesn't look like a foreign language, okay? That you can get used quickly, but of course, not everything is the same.