Lesson 1 - Introduction to Swift, the platform and Xcode
Swift could be briefly described as the programming language of the Apple's world. It was introduced in 2014 as the Objective-C replacement. By design, it's an object-oriented compiled language with type control. It's relatively close to such languages as Java, C# and similar. Because it's a relatively new language, it doesn't carry many flaws from its past and offers a number of modern approaches. During the development, it was also thought of the bunch of code written in Objective-C, so Swift contains a few compromises in favor of this language.
Swift is open-source since the version 2.2 and since Swift 3, Apple makes it backward compatible. If you've encountered the language in the past, you may have run into problems with new releases which could cause the old code not to work. Although Swift is open-source and you can program in it e.g. on Linux, it's still mainly a language for Apple devices. Do you want to program an iPhone app? Use Swift. Do you want to program a terminal utility for your MacBook? Again Swift. Do you have Apple Watch and want to make a mini app for it? Of course, use Swift again. Swift is also beginning to infiltrate the world of websites and web applications. The Vapor framework is widespread and offers a modern way to develop for web. The authors say in a bit of a cliché way, that it's "the future of web development". Anyway, Vapor looks good and if you're interested in this topic, check out the Vapor official website.
The standard of Swift development is the Xcode IDE, which Apple provides for free through AppStore. Just download it and you're ready. Xcode is a comprehensive development environment and offers everything you need to create your applications. In addition to the code editor, the designer will help you create UI, it'll run your app on a physical device or in a simulator, and then it'll help you upload it to the AppStore.
You may know console applications from other languages, which are used mainly
in the introduction to test your code. This exists in Swift (and Xcode) as well,
but there is also a simpler way to test your code called Playground. It's
literally a playground where you can test any part of Swift without having to
create a traditional project and then run it. This course will be dedicated to
console applications. We'll get to form applications in the following course.
You can create a Playground after running Xcode by choosing Get started with
a playground and selecting where to save the single file on the disk. The
editor window will show up, where you can start writing the code. The code will
start to compile immediately and you'll see real-time results on the right. For
example, if you assign a
12 + 4 expression to an
variable, you'll see
16 on the right. What is a variable and what
we can do with it is explained in the next lesson.
However, Playground has one major disadvantage; it's not possible to retrieve user inputs there reasonably. Therefore, during the course, we'll sometimes create console applications which allow us to enter and respond to the user inputs. We'll use Playground mainly to test basic code, but feel free to choose the approach you like.
Command Line Tool
At the end of this introductory lesson, we'll create a console application that allows us to enter and respond to keyboard input. Run Xcode and choose Create a new Xcode project. After that, you must select macOS as the platform and finally choose the Command Line Tool. Alternatively, you can use the search box in the top right corner.
In the next step, you just type the name of your new project and check whether Swift is set as the language. After creating the project, you'll see the fully-featured Xcode environment. You don't need to know anything yet, just focus on the left side where you can see your project files.
It's been a tradition that the first program in a new language is called
Hello world. It's a program that will print a "Hello world" message to the user
or any similar text in any way. Click on
main.swift to open the
file in the editor. This is the file which we're going to write our code into,
today and in the other lessons as well. The code will be executed when we run
it. By default, there are two lines of code that already represent the famous
Hello world program:
import Foundation print("Hello, World!")
The first line simply imports the most important Swift libraries so we could even do anything.
The second line prints the famous greeting to a special window which you can
find below the editor. That's also where we'll enter keyboard inputs in the
print() is a function that prints text to the
console. Functions can have input parameters which we pass through the
parentheses, we separate them by commas. In the case of the
function, the parameter is the text we want to print. We're going to refer to
texts as to strings and wrap them in quotation marks so Swift would understand
them and didn't misinterpret them as some other commands. You can try to edit
the text and print your own message. Of course, we won't include this printing
line in our further programs. However, we'll keep the
import Foundation line for every other program in this course.
This will be enough for today. You are now friends with Xcode and know how to run Swift code.
In the next lesson, Variables, type system and parsing in Swift, it'll get more interesting. We'll learn how to work with variables and read user inputs.
Downloaded 147x (36.31 kB)
No one has commented yet - be the first!