Swift Environment settings
before writing the SWIFT process, you need to set up the swift development environment first. Swift provides a playground platform for learning purposes, and we also need to set it up. Swift coding needs to be provided to Xcode software in playground. If you are familiar with the swift concept, you can use the Xcode IDE to develop the Iso/os x application.
First, you need to register an account on the Apple Developer website (This tutorial assumes you have a developer account). If you are logged on to the Apple website, please visit the following link:
Download for Apple Developers
This will list a number of available software as follows:
Now select Xcode and click on a nearby disc image to be downloaded from a given link. When the DMG file is downloaded, you can install it with a simple double-click, and follow the instructions given to perform the installation. Finally, follow the instructions given and move the Xcode drag-and-drop icon into the application folder. First agree to the relevant agreement:
Enter the administrator password, as shown in the following illustration, and then install the start:
Now that we've installed the Xcode on the machine, we can start Swift's code. Next, open the application folders available in Xcode and continue to accept the terms and conditions. Playground
Select the get started with a playground option and enter the name of the playground and select as the IOS platform. Finally, you will get the playground window as follows:
The following is the code used by the default Swift playground window.
Copy Code code as follows:
var str = "Hello, playground"
If you create the same program in an OS X program, it will include the import Cocoa the same program looks as follows:
Copy Code code as follows:
var str = "Hello, playground"
When the above program is loaded, it should display the following results in the playground result area (right hand side).
Congratulations, the SWIFT programming environment is ready to continue to learn the following from Swift.
Swift Basic syntax
we started with the following OS Xplayground created by hello,world! program, which includes the import Cocoa, as follows:
Copy Code code as follows:
Import Cocoa
/* My I/I in Swift *
var myString = "Hello, world!"
println (myString)
If you create the same program for IOS playground and then include the import Uikit statement, the program looks like this:
Copy Code code as follows:
Import Uikit
var myString = "Hello, world!"
println (myString)
When we use the appropriate playground to run the above program, we get the following results:
Hello, world!.
Now look at the basic structure of the SWIFT program, so this is the basic building block for understanding the swift programming language.
Import in Swift's use
You can use the import statement to directly import any OBJECTIVE-C framework (or C library) to the SWIFT program. For example, the above import cocoa statement makes all cocoa libraries, APIs, and forms all OS X development layers that can be used at run time in Swift.
Cocoa is implemented in objective-c as a superset of C, so it is easy to mix C or even C + + into Swift applications.
The use of tokens in Swift
The SWIFT program consists of various tokens, which can be a keyword, identifier, constant, string literal, or symbol. For example, the following Swift statement consists of three tokens:
Copy Code code as follows:
println ("test!")
The individual tokens are:
println
(
"Test!"
)
Comments
Annotations are like help text in Swift programs that are ignored by the compiler. Multiline comments begin with/* and end with the */character, as shown in the following illustration:
Copy Code code as follows:
/* My I/I in Swift *
Swift can nest multiple lines of comments. The following are valid comments by Swift:
/* My The In Swift is Hello, world!
/* Where as Second program is Hello, swift! */
A single comment is written at the beginning of the comment using//.
Copy Code code as follows:
My the program in Swift, this is a line of comments
Semicolon
Swift does not require the semicolon to be used (;) after each statement of the code, although it is optional, but if the semicolon is used, then the compiler will not have any effect.
However, if you are in the same row, separated by semicolons as delimiters, the compiler will throw a syntax error. You can rewrite the hello,world! above The procedure is as follows:
Copy Code code as follows:
Import Cocoa
/* My I/I in Swift *
var myString = "Hello, world!"; println (myString)
Identifier
The Swift identifier is the name used to identify a variable, function, or any other user-defined item. An identifier begins with the letter A through Z or a to Z or underscore _ followed by 0 or more letters, underscores and numbers (0〜9).
Swift does not allow punctuation characters, such as @,$ and%, in identifiers. Swift is a case-sensitive programming language. Therefore, manpower and manpower are two different identifiers in Swift. Here are some examples of acceptable identities:
Azad Zara ABC move_name a_123
myname50 _temp J a23b9 RetVal
To use a reserved word as an identifier, you need to refer to it in reverse quotation marks (') before and after the identifier. For example, class is not a valid identifier, but ' class ' is valid.
Key words
The following keywords are reserved in Swift. These reserved words are not used as constants or variables or any other identifier names unless they are escaped with inverted quotes:
Using keywords in a declaration
Use keywords in statements
Using keywords in expressions and types
Keywords to use in specific situations
Space
Contains only spaces, may have comment lines, is called a blank line, and the Swift compiler completely ignores it.
The space is Swift used to describe spaces, tabs, line breaks, and annotation terms. A space separates a declaration into another part, allowing the compiler to identify elements in a declaration, such as int, end, and the next element to begin. So, in the following statement:
Copy Code code as follows:
There must be at least one whitespace character (usually a space) between Var and age so that the compiler can differentiate between them. In addition, the following statement:
Copy Code code as follows:
int fruit = apples + oranges//get the total fruits
Fruit and =, or = and apples whitespace characters are not required, but you can add some spaces if you want to use it for readability purposes.
Literal amount
Literal is the source representation of a value for an integer, floating-point number, or string type. The following are examples of literal values:
Copy Code code as follows:
Literal//Integer
4.24159//Floating-yiibai literal
"Hello, world!." String literal