Programare swift Swift tutorial from tutorials point | Page 18
3. Swift 4 – Basic Syntax
Swift 4
We have already seen a piece of Swift 4 program while setting up the environment. Let's
start once again with the following Hello, World! program created for OS X playground,
which includes import Cocoa as shown below:
import Cocoa
/* My first program in Swift 4 */
var myString = "Hello, World!"
print(myString)
If you create the same program for iOS playground, then it will include import UIKit and
the program will look as follows:
import UIKit
var myString = "Hello, World!"
print(myString)
When we run the above program using an appropriate playground, we will get the following
result.
Hello, World!
Let us now see the basic structure of a Swift 4 program, so that it will be easy for you to
understand the basic building blocks of the Swift 4 programming language.
Import in Swift 4
You can use the import statement to import any Objective-C framework (or C library)
directly into your Swift 4 program. For example, the above import cocoa statement
makes all Cocoa libraries, APIs, and runtimes that form the development layer for all of
OS X, available in Swift 4.
Cocoa is implemented in Objective-C, which is a superset of C, so it is easy to mix C and
even C++ into your Swift 4 applications.
Tokens in Swift 4
A Swift 4 program consists of various tokens and a token is either a keyword, an identifier,
a constant, a string literal, or a symbol. For example, the following Swift 4 statement
consists of three tokens:
print("test!")
6