Swift BASICS (I)

Source: Internet
Author: User

1: Hello World

import Foundationprintln("hello world")

In this way, a piece of code can print out Hello world without adding ";" if you add ";", it can also run. It seems that it is to avoid the trouble. It is no longer like the C language. ";" is added at the end of every sentence of code. Similarly, in philosophy, every sentence of code is added "; "equal to not adding"; ", but writing every time is troublesome!

2: code comments, the same as the C language "//" and "/**/"

3: println () Output

If you want to output variables

VaR ftmp = 8.9 println ("\ (ftmp)") // the formatting of println is done by "\ ()". This is a omnipotent thing, replace the C language printf () with % s, % d, % C, % F to format and write parameters. I guess the reason is that he can reason for the type of the variable, so he formatted the data type himself,
 

4: constants and variables

You must declare before use. The statement uses ":"

var strMsg: StringstrMsg = "I‘m swift"
VaR strmsg: string? // Nil

 

Let IMAX = 10 // constant, which must be initialized and cannot be modified again
// IMAX = 9 Compilation failed. It may be similar to const modification in C language.
VaR imini = 3 // variable, which can be not initialized
VaR itmp: int32 // variable declaration. Itmp = 44 not initialized // variable assignment

Here, swift uses the reasoning type for the data type, and its reasoning type also complies with the compatible rules for data type conversion in C language.

VaR ftmp = 8.9 ftmp = 9 println ("\ (ftmp)") // here the reasoning is that ftmp is of the float type, so here when ftmp = 9, you can convert integer 9 to float. So you can see that the output is 9.0.
But not vice versa.

VaR ftmp = 8

// Ftmp = 9.02 compilation failed.

 

Multiple variables can be defined at a time.

VaR itmp = 9, itex = 9.8 // here, the type inference of each variable is independent. // Itmp = 8.99 error // itmp is deduced as Int. The itex reasoning is float.

VaR red, green, blue: Double // indicates that all three variables are of the double type.

 

Variable names and constant names support Unicode. In this way, Chinese characters or other characters can be used, but I don't think so.

5: int Data Type

Int data indicates the range. Max can be used.

let minValue = UInt8.min  // minValue is equal to 0, and is of type UInt8let maxValue = UInt8.max  // maxValue is equal to 255, and is of type UInt8

Int indicates the int64 of int32 and 64-bit systems based on the system.

Uint indicates uint32 and uint64 in 64-bit systems based on the system.

let decimalInteger = 17let binaryInteger = 0b10001       // 17 in binary notationlet octalInteger = 0o21           // 17 in octal notationlet hexadecimalInteger = 0x11     // 17 in hexadecimal notation

Type conversion

VaR dbvar: doubledbvar = 9.9let Ivar = int (dbvar) println ("\ (Ivar)") var strint = "123" Var Ivar = strint. if toint () is successful, Ivar returns int; otherwise, Nil is returned.

6: bool this type is clear as "true" and "false"

If statement only checks bool type

Let iflag = 1if iflag {// compilation failed, 1 is not a logical value}
Let iflag = 1if 1 = iflag {// Yes}

7: Use () to enclose the tuples

let tupError = (500, "httpError")let (errorCode, errorMsg) = tupErrorprintln("\(errorCode)")//500println("\(errorMsg)")// httpError

You can also define the name tuples.

let tupError = (errorCode :500, errorMsg:"httpError")println("\(errorCode)")//500println("\(errorMsg)")// httpError

8: Nil Swift's nil is different from other languages. For example, OC indicates a null pointer, while swift indicates no value. The type is not limited to the pointer type. Nil can be set to any type in swift

var iFlag :Int?iFlag = niliFlag = 9
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.