Variables

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!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末赏胚,一起剝皮案震驚了整個(gè)濱河市呕臂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖砰嘁,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異亩歹,居然都是意外死亡饼记,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門荧嵌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呛踊,“玉大人,你說我怎么就攤上這事啦撮√吠” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵赃春,是天一觀的道長愉择。 經(jīng)常有香客問我,道長织中,這世上最難降的妖魔是什么锥涕? 我笑而不...
    開封第一講書人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮抠璃,結(jié)果婚禮上站楚,老公的妹妹穿的比我還像新娘。我一直安慰自己搏嗡,他們只是感情好窿春,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著采盒,像睡著了一般旧乞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上磅氨,一...
    開封第一講書人閱讀 51,245評(píng)論 1 299
  • 那天尺栖,我揣著相機(jī)與錄音,去河邊找鬼烦租。 笑死延赌,一個(gè)胖子當(dāng)著我的面吹牛除盏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播挫以,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼者蠕,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了掐松?” 一聲冷哼從身側(cè)響起踱侣,我...
    開封第一講書人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎大磺,沒想到半個(gè)月后抡句,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杠愧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年待榔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片流济。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡究抓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出袭灯,到底是詐尸還是另有隱情,我是刑警寧澤绑嘹,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布稽荧,位于F島的核電站,受9級(jí)特大地震影響工腋,放射性物質(zhì)發(fā)生泄漏姨丈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一擅腰、第九天 我趴在偏房一處隱蔽的房頂上張望蟋恬。 院中可真熱鬧,春花似錦趁冈、人聲如沸歼争。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沐绒。三九已至,卻和暖如春旺坠,著一層夾襖步出監(jiān)牢的瞬間乔遮,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來泰國打工取刃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蹋肮,地道東北人出刷。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像坯辩,于是被迫代替她去往敵國和親馁龟。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,489評(píng)論 0 23
  • 1.自己存在的問題 自己對(duì)公司對(duì)產(chǎn)品比較相信濒翻,但是個(gè)人缺乏實(shí)戰(zhàn)經(jīng)驗(yàn)屁柏,對(duì)代理提出的問題沒法解答,所以沒有說服代理升級(jí)...
    雪cddthc閱讀 237評(píng)論 0 0
  • 早上在家聽故事有送,到校跟著二樓鋪被子淌喻,由門衛(wèi)帶去操場!兒子還是那副不開心的表情但又表現(xiàn)得很聽話雀摘。 不放心在圍欄外看裸删,...
    紅棗喝閱讀 232評(píng)論 0 0
  • 第一次見到Carol,她真的超級(jí)可愛阵赠,雖然已經(jīng)六十幾涯塔,皮膚松弛身材走樣,但她的眼睛卻很清亮清蚀,一閃一閃像個(gè)孩子匕荸。她的...
    一棵樹白閱讀 216評(píng)論 0 0
  • Bootstrap的導(dǎo)航條下拉菜單為了適應(yīng)移動(dòng)設(shè)備沒有鼠標(biāo)hover的狀態(tài),都是點(diǎn)擊彈出下拉菜單枷邪,為了適應(yīng)一般網(wǎng)站...
    汪南閱讀 397評(píng)論 0 0