Swift Basic Syntax (i)

Source: Internet
Author: User

Swift is a new OS X/ios programming language based on the evolution of OBJC, and OBJC is a programming language based on the C language. So theoretically OBJC and C + + is the same generation of products and OBJC and C + + are independent of the two sets of systems, know that later you will not be difficult to understand why OBJC and C + + mixed to change the file name to mm and OBJC and C mixed without modifying the file name

Constants and variables

Constants are defined by let in swift, and variables are defined with VAR

Defining formats

  1  //   //  constant   3  let identifier: Data type?  //  variable   5  var identifier: data type?  //  Definition and assignment   //  constant   Let identifier: Data type =  9  //  variable  10  var identifier: Data type = value 

In particular, the swift identifier is expressed in Unicode character encodings, meaning that you cannot define variables in numbers or sensitive fields, and that the rest of the text is used in a very different way from OBJC and C + +. Identifiers for OBJC and C + + are implemented using Ascⅱ code. Having known the principle, I did an experiment.

// function Declaration func test function (text:string) {        println ("\ (text)")    }// function call test function ("22222")

Compile, execute everything OK

Basic type

Basic type this piece of Swift is compatible with the OBJC type I only introduce a new type

Number Type

Swift's number types are integer/int/float/double

The integer type is actually the Int8 type in OBJC.

The int/float/double is based on the number of CPU bits running the machine as the variable bits. For example, on IPhone5 int is Int32 and on iphone5s int is Int64 and so on

Numbers indicate:

Decimal: Direct write numbers such as Var a =10

Binary: 0b plus numbers such as Var a=0b1010

Octal: 0o plus numbers such as Var a=0o12

Hex: 0x plus numbers such as Var a= 0x0A

The decimal representation of these four variables is 10

Type alias (Typealias)

The Typealias in Swift is equivalent to the typeof in the C language.

Boolean type

The bool in swift corresponds to only two values of the bool in OC: TRUE, False

The conditions in swift determine that the return is bool.

// in C, we can do this with If. int a=1; if (a)    printf ("%d", a); // but we put the same code in swift. var a:int=1if  a{    println (a)//error}

Ganso (tuples)

Tuples in Swift when I first saw him, I thought he was an array (I x, arrays are not array?). What the hell is tuples? look carefully not. It can be used both as an array and as a strongly typed dictionary, and in the official document it is primarily intended for function return. In fact, I think it is a multi-functional structure (c + +), the use of simple definition at any time

//definition Mode 1Let A= ( the," Age")//Outputprintln (A.0)//Output Ageprintln (A.1)//definition mode twoLet b= (name:"Tony", Age: the)//Output Name:tony age:15println"name:\ (a.name) age:\ (a.age)")
Optional type (optionals)

Optionals is not so much a basic type as it is a necessary attribute for each base type, if there is a value optionals true, the Optionals is false.

var a="123"if let b=a.toint () {    println (b!)}

If a variable is added directly after the IF, then the variable is judged to have no value. The exclamation mark after B is the B not nil ToInt () function is to convert the string to int similar to what you can do if you remove the 123 and change it to hello.

Debugging specialized functions

Debug Private Function assert (condition, string) if the condition does not set the output string

Write so much today, if you have any mistakes please advise. I appreciate it.

Swift Basic Syntax (i)

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.