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!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,634评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,951评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,427评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,770评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,835评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,799评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,768评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,544评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,979评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,271评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,427评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,121评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,756评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,375评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,579评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,410评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,315评论 2 352

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,462评论 0 23
  • 1.自己存在的问题 自己对公司对产品比较相信,但是个人缺乏实战经验,对代理提出的问题没法解答,所以没有说服代理升级...
    雪cddthc阅读 234评论 0 0
  • 早上在家听故事,到校跟着二楼铺被子,由门卫带去操场!儿子还是那副不开心的表情但又表现得很听话。 不放心在围栏外看,...
    红枣喝阅读 232评论 0 0
  • 第一次见到Carol,她真的超级可爱,虽然已经六十几,皮肤松弛身材走样,但她的眼睛却很清亮,一闪一闪像个孩子。她的...
    一棵树白阅读 214评论 0 0
  • Bootstrap的导航条下拉菜单为了适应移动设备没有鼠标hover的状态,都是点击弹出下拉菜单,为了适应一般网站...
    汪南阅读 394评论 0 0