Swift Basics

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 One : swift basics

We just created a variable, called it name and gave it a value. Variables are created with the keyword 'var', followed by a name and a value, which is assigned with '='

var task = "learn java"
task = "learn swift"
print(task)  ==>  learn swift

Just like I cant't handle more than one of these tasks at once, variables can only remember one value at a time.
If you want to give your variable a different value, simply use its name (without 'var')


Programs are written in code. That's another word for lists of instructions. With code, we can build great apps, games and websites. Sit tight, Let's look at the following:

var love = "I love"
var cookies = "peanut biscuit"
print(love + cookie + "!")  ==>  I love peanut biscuit!

The word print followed by '( )' reveals the output, specified by the string inside the brackets.
Psst:print( ) is called a function


Variables can store lots of different things. We've already seen that we can store strings. But wait : numbers can be stored as well!

var myString = "4"
var myNumber = 4

We can see: myString holds a string and myNumber has the value 4.
It's important to notice that there's a difference. If you want a variable to hold a meaningful value, just use a number without "quotation marks".


Numbers come in different types. Integers can hold whole numbers, Doubles can hold numbers with a decimal place.
If you assign a number to a variable, the variable automatically knows which type of number it can hold.

var myInteger = 2
var myDouble = 2.0
myInteger = 10(正確)
myInteger = "10"(X)
myInteger = 10.0(X)

The variable myInteger has the value 2 assigned to it. From now on, it will only take Integer values. "10"(string) and 10.0(Double) won't work.


With code, we can also do math!

var five = 5
var six = 6
var result = five + six
print(result)  ==>  11

We can do basic math operations with '+', '-', '*', '/'. Our beloved print( ) function doesn't just work with strings but with numbers as well!


We can change the values of variables at any time. If we know that the value of a variable won't change, it's a good idea ti use let instead
My name is 'dabaodeaiqing' and I sure hope that'll stay this way. so:

let name = "dabaodeaiqing"

then my name is a constant and can't be tinkered with!


If we don't want to assign a value to a variable upon creation, the variable doesn't know what type of data it can hold. In that case, we need to give the variable a type.

var myString: String
var myInteger: Int
var myDouble: Double

We've created three variables that can hold three different types of data. Stay tuned, we''ll talk about many more types in Swift!


We've already learned a lot about programming. We know how variables are created, what the difference between var and let is, how to combine strings and how to peint something out.

var numberVar = 42
var stringVar: String
let pt1 = "Let's learn swift"
let pt2 = "happily!"
var output = pt1 + pt2
print(output)  ==>  Let's learn swift happily!

Thanks a lot!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市鸟赫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌官脓,老刑警劉巖站削,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡逐样,警方通過查閱死者的電腦和手機(jī)泳桦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門汤徽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人灸撰,你說我怎么就攤上這事谒府。” “怎么了浮毯?”我有些...
    開封第一講書人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵完疫,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我债蓝,道長(zhǎng)壳鹤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任饰迹,我火速辦了婚禮芳誓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘啊鸭。我一直安慰自己锹淌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開白布赠制。 她就那樣靜靜地躺著赂摆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上烟号,一...
    開封第一講書人閱讀 52,394評(píng)論 1 310
  • 那天曲楚,我揣著相機(jī)與錄音,去河邊找鬼褥符。 笑死龙誊,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的喷楣。 我是一名探鬼主播趟大,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼铣焊!你這毒婦竟也來了逊朽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤曲伊,失蹤者是張志新(化名)和其女友劉穎叽讳,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坟募,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡岛蚤,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了懈糯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片涤妒。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖赚哗,靈堂內(nèi)的尸體忽然破棺而出她紫,到底是詐尸還是另有隱情,我是刑警寧澤屿储,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布贿讹,位于F島的核電站,受9級(jí)特大地震影響够掠,放射性物質(zhì)發(fā)生泄漏民褂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一祖屏、第九天 我趴在偏房一處隱蔽的房頂上張望助赞。 院中可真熱鬧,春花似錦袁勺、人聲如沸雹食。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)群叶。三九已至吃挑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間街立,已是汗流浹背舶衬。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赎离,地道東北人逛犹。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像梁剔,于是被迫代替她去往敵國(guó)和親虽画。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359

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