Swift Fundamentals: Part I: basic data types and structures

Source: Internet
Author: User
Tags pears

First of all, a little happy: Today is Tuesday, Guangzhou, the weather is particularly clear, the morning work of the mood is not the same, the most worthy of happy things, soon to 51 Labor Day, said Labor Day, holiday is it. You know. To talk about my work last week, last week can be said black wheel pressure city to destroy, a light to gold scale open, last week the company plans to complete the project on-line within a week, may be asked, how can a week within a project, hehe, a week in theory is impossible to complete a project, We just made a semi-finished product at the boss's request, posted on-line, and looked at the user's feedback and experience on our software. Hope that the user can give us more suggestions, we can make our products better, even more. Back to say I myself, I am also very lucky, overtime to nine points, Saturday also do not rest, at night to go back to optimize their own code, but also learn some new technology, pressure is essential. Well, let's not talk about this, we'll learn Swift's high-level language.

1. Open your Xcode and create a new Playgound file

(1) Output statement;

var varible = "Hello,playground"

Print (varible)

Print ("Hello,playground");

Note: In the swift language, add; and no add all represent a separate statement.

(2) Constants and variables: constants are modified with let, and variables are modified with VAR

Let Const = 40

Let name = "This is constant";

Let width = 76.0;

var varii = 45;

(3) If the constant or variable has no initial value or the initial value is ambiguous, the type needs to be specified

var age:float = 23

Let mycontst:string = "dddddd"

(4) Types of conversions

(4.1)

let apples = 5;

Let pears = 7;

Let str = "I has \ (apples) Apple and \ (pears) Pear"

(4.2)

let apples = 5;

Let pears = 7;

Let str = String (apples) +string (pears);

(5) Use [] to create arrays and dictionaries, and to access elements through subscripts and keys

(5.1) array

var goodlist = ["Shoes", "coats", "Towers", "Candy", "trousers"]

Print (goodlist[1]);

goodlist[0]= "Shoe"

Print (goodlist)

(5.2) Dictionary

var dic = [

"Name": "Zhangsan",

"Age": 23,

"Sexal": "Male",

]

Print (dic["name"])

Dic["age"] = 24

(6) Control flow: Use if and switch to perform conditional operations, using for-in, for, while, and do-while for looping. Wrapping conditions and loop variable parentheses can be omitted, but curly braces on the body of the statement are required.

(6.1) for in control flow

Let Indivadulscore = [70,66,80,90,56,34]

var score = 0

For S in Indivadulscore

{

If s>60

{

Score + = 20

}else

{

Score + = 100

}

}

Note: Look at the differences in the syntax of the control flow you wrote earlier

(6.2)? indicates an optional

var optionstring:string? = "Hello,world"

Optionstring = = Nil;

Print (optionstring)

var optionname:string? = "Welcome,please"

var ogreeting = "Hello"

If let name = Optionstring

{

ogreeting = "hello,\ (optionname)"

}

(6.3) Branch switch statement

Let cons = "red paper"

Switch Cons {

Case "red paper":

Let vegetablecomment = "Add some raisins and make ants on a log."

Case ' White Paper ':

Let vegetablecomment = "I don't want to eat"

Default

Let vegetablecomment = "everything tastes good in soup."

}

(6.4) for in traversal array

Let Numberarr = [

"Zuqiu": [2,5,7,9,4,2,1],

"Yumaoqiu": [1,2,3,4,5,6,7],

"Lanqiu": [1,5,6,4,3,6,8],

"Taiqiu": [34,51,12,3,5,6,9],

]

var Interest = 0

for (kind,numbers) in Numberarr

{

For number in numbers

{

If number < Interest

{

Interest = number;

}

}

}

(6.5) Use while to repeatedly run a piece of code until the condition is not met. The loop condition can be at the beginning as well as at the end.

var n = 2

While n < 100

{

n = n * 2

}

(6.6) Do {}while Just change do to repeat

var m = 2;

Repeat {

m = m * 2

}while m < 100

M

(6.7) can be used in the loop. To represent a range

var secondforloop = 0

For i in 0.. < 3 {

Secondforloop + = 1

}

Secondforloop

Swift Fundamentals: Part I: basic data types and structures

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.