SWIF Basic Syntax 01

Source: Internet
Author: User

Import Foundation

/**

* 1, the first Swift program

*/

println ("Hello, world!")

/**

* 2, define constants

*/

Let a =10

Let Cinteger:double =12.00

/**

* 3, define Variables

*/

var b =0

b =11

var bfloat:float

Bfloat = 11.0

println (a)

/**

* 4, type conversion (concatenation of strings)

*/

Let label = "the width is"

Let width =94

Let Widthlabel =label + String (width)

Concatenation of strings

Let WidthLabel1 = "\ (label) \ (width)"

println (Widthlabel)

println ("type conversion: \ (WIDTHLABEL1)")

/**

* 5, define Array

*/

var array = ["hehe", "haha", "Nimei"]

println (Array)

println (Array[0])

Array[0] = "Kebe"

println ("Array: \ (array)")

Defines an empty array of type uncertainties

var array2 = []

Array2 = [up]

println (Array2)

println (Array2[1])

/**

* 6, define Dictionary

*/

var dictionary = ["Xiaoming": "Ten", "Xiaohong": "8", "Xiaofang": "12"]

println (dictionary)

println (dictionary["Xiaoming"])

dictionary["xiaoming"] = "20"

println (dictionary)

Defines an empty dictionary of type uncertainties

var dict2 = [:]

Dict2 = [2: "2", 3:3]

println (DICT2)

/**

* 7, Process Control

*/

Let DataArray = [1,2,3,4,5,6]

var sore =0

For Soretin dataarray{

If Soret > 3{

Sore + = 5

}else{

Sore-= 2

}

}

println (sore)

var count = 0;

Forvar i = 0;i <3;++i{

Count + = 1;

}

println ("count=\ (count)")

var firstforloop =0

For Iin 1...4 {

Firstforloop + = i

}

println ("firstforloop=\ (Firstforloop)")

Func greet (name:string,day:string)->string{

Return "hello,\ (name), Today is \ (day)"

}

Let look = Greet ("Kobe", "Monday")

println ("\ (look)")

/**

Use tuple (tuple) to return multiple values

*/

Func GetPrice (), (double,int,float) {

Return (3.33,3,3.3)

}

Let A = GetPrice ()

println ("\ (GetPrice ())")

/**

* Functions can accept variable parameters, collected into an array

*/

Func sumof (Numbers:int ...) ->int{

var sum = 0

For number in numbers{

Sum + = number

}

return sum

}

Let Suma =sumof ()

Let Sumb =sumof (12,12,123)

println ("suma=\ (suma)")

println ("Sumb =\ (sumb)")

/**

* Objects and classes

*/

Class Shape {

var numberofsides = 0

Func simpledescriptio ()->string{

Return "A shape wiht \ (numberofsides)"

}

}

var shape =shape ()

Shape.numberofsides =7

Let Shapdes = Shape.simpledescriptio ()

println ("Shapdes =\ (shapdes)")

/**

* Use the Init constructor to instantiate a class

*/

Class Shape1 {

var numberofsides = 0

var name:string

Init (name:string) {

Self.name = Name

}

Func simpledescriptio ()->string{

Return "A shape wiht \ (numberofsides)"

}

}

var shape1 =shape1 (name: "Wang")

println ("Shape1 =\ (shape1.name)")

SWIF Basic Syntax 01

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.