Variables

2016-12-06  本文已影响0人  大宝的爱情

Swift’s icon is like a swallow which is flying! Follow me, learn swift one by one, I believe we can fly!

(sort out it by internet,any similarity is purely coincidental)

Someday, I might add translations(😀😀😀😀😀).

Begin Two:Variables

Variables (including constants) have to be declared before we can use them. A declaration consists of var or let and the name of the variable:

let mySwift

remember: Every variable need to be declared before we can use it!


If the value of a variable won't change, it's a good idea to make it a constant.

let mySwift1 = 10
var mySwift2 = 0

Now, only the value of 'mySwift2' can be changed because mySwift1 is a constant. It's good practice to use let by default and only go for var in case you need to change a variable's value.


Once we have declared a variable, we can't change the data type it can handle. Yup, modification is impossible!

let mySwift3 = 10
var mySwift4 = 0
mySwift4 = 1(正确)
mySwift4= 1.0(X)

The variable mySwift4 is an Integer and we can't give it a value that isn't of that type. This is called 'type safety'


If we give our variable a value upon creation, the variable automatically knows its type. That's called 'type inference'

let mySwift5 = 1.0
var mySwift6 : Int

If we don't want to assign a value at the time of creation, we need to specify the variable type.


MySwift 7 is declared as an Integer constant and initialized with a value of 10, mySwift8 gets declared as a variable of type Int. Before we can use it, we need to initialize it as well!

let mySwift7 = 10
var mySwift8 : Int
mySwift8 = 4
print(mySwift8)  ==>  4

we can only do stuff with a variable after declaration and initialization!


We want to add a value(value = 2) to myNumber. so:

var myNumber = 4
myNumber += 2
print(muNumber)  ==>  6

If you want to subtract 2, it's almost the same thing: myNumber -= 2.


Tuples are combinations of values that always appear together:

let person = ("dabaodeaiqing",25)
print(person.0)  ==>  dabaodeaiqing
print(person.1)  ==>  25

If we want to access a tuple, we use its name followed by .0 for its first member, .1 for the second, .2 for the third, and so on.

Often times, we want wo refer to tuple members with a name, not just with .0, .1 and so on.

let person = ("xiaobaodeaiqing",25)
let (name, age) = person
print(name)  ==>  xiaobaodeaiqing
print(age)  ==>  25

We can access tuple members by name! With a named tuple, it's way easier to understand what's in it.

Let's name our tuple! That'll make our code even more readable.

let person = (name:"baobaodeaiqing", age:23)
print(person.name)  ==>  baobaodeaiqing
print(person.age)  ==>  23

Because of 'type inference', we don't have to specify the type of our tuple members!


Sometimes, we want to declare a variable that holds .. nothing. That;s possible, too!

var myFriends : String?

The question mark indicates that there might be no value inside. This type if variable is called an optional.

'nil' stands for 'no value'. It might seem weird but nil can be assigned like any other value.

var myFriends : String?
myFriends = nil

At the time of its creation, an optional is nil by default.

If we want to give our optional an initial value other than nil, there's a way:

var myFriends : String? = "xiaobaodeaiqing"
myFriends = nil
myFriends = "xiaobaodeaiqing"

In order to check if there's a value, we create a constant that takes the values form the optional(if there is one)

var myFriends : String? = "xiaobaodeaiqing"
if let someone = myFriends{
        print("hello" + someone)  ==>  hello xiaobaodeaiqing
}

'if let' creates a constant only if the optional holds a value; otherwise, the code block inside '{ }' won't be wxecuted!


Let's recap what we've learned!

let person = (name:"xiaobaodeaiqing",age:23)
var myFriends : String?
myFriends = person.name
if let someone = myFriends{
        print(someone)  ==>  xiaobaodeaiqing
}

Always declare and initialize your variables before you put them to use!


Thanks a lot!

上一篇下一篇

猜你喜欢

热点阅读