- 作者: Liwx
- 郵箱: 1032282633@qq.com
- 源碼: 需要
源碼
的同學(xué), 可以在評論區(qū)
留下您的郵箱
iOS Swift 語法
底層原理
與內(nèi)存管理
分析 專題:【iOS Swift5語法】00 - 匯編
01 - 基礎(chǔ)語法
02 - 流程控制
03 - 函數(shù)
04 - 枚舉
05 - 可選項(xiàng)
06 - 結(jié)構(gòu)體和類
07 - 閉包
08 - 屬性
09 - 方法
10 - 下標(biāo)
11 - 繼承
12 - 初始化器init
13 - 可選項(xiàng)
目錄
- 00-Playground
- 01-Playground簡單使用
- 02-新建子Playground Page
- 01-HelloWorld
- 02-UIView
- 03-UIImageView
- 04-UITableViewController
- 05-注釋
- 06-Markup
- 07-常量
- 08-標(biāo)識(shí)符
- 09-常見數(shù)據(jù)類型
- 10-字面量
- 11-類型轉(zhuǎn)換
- 12-元組(Tuple)
00-Playground
Playground
是 Apple 在 2014 年 WWDC 發(fā)布 Swift 的時(shí)候嗤形,和 Xcode 一起發(fā)布的用于實(shí)時(shí)調(diào)試 Swift 代碼的小工具湿镀。
本專題中【iOS Swift5語法】的大部分代碼都通過 Playground 來演示蜻牢,它可以實(shí)時(shí)的為我們編譯代碼,并自動(dòng)的顯示每一行代碼的結(jié)果竟秫,他能演示很多代碼場景。Playground是我們學(xué)習(xí) Swift 一個(gè)必不可少的的利器。
【iOS Swift5語法】 專題使用的Xcode版本已更新至Xcode最新版本Xcode 11.4.1
航揉。
01-Playground簡單使用
-
Playground快捷鍵
- 顯示側(cè)邊欄:
Command + 1
- 隱藏側(cè)邊欄:
Command + 0
-
Playground
可以快速預(yù)覽代碼效果
,是學(xué)習(xí)語法的好幫手 -
Command + Shift + Enter
:運(yùn)行整個(gè)Playground -
Shift + Enter
:運(yùn)行截止到某一行代碼
- 顯示側(cè)邊欄:
新建Playground
運(yùn)行Xcode驶乾, 選中頂部菜單File -> New -> Playground ...
02-新建子Playground Page
-
新建
子Playground Page
-
選中PlaygroundTest,右鍵選擇
New Playground Page
image.png 重命名子Playground Page為
01-HelloWorld
-
- Playground文件目錄說明
01-HelloWorld
-
不用編寫main函數(shù)
, Swift將全局范圍內(nèi)的首句可執(zhí)行代碼作為程序入口 - 一句代碼尾部可以
省略分號(hào)(;)
,多句代碼
寫到同一行時(shí)必須用分號(hào)(;)隔開
- 用
var定義變量
邑飒,let定義常量
,編譯器能自動(dòng)推斷
出變量\常量的類型
print("Hello World!")
let a = 10
let b = 20
var c = a + b
c += 30
02-UIView
使用UIKit框架需要導(dǎo)入
UIKit
框架,import UIKit
PlaygroundSupport
框架支持實(shí)時(shí)預(yù)覽
UI效果轻掩,需要實(shí)時(shí)預(yù)覽UI效果需導(dǎo)入PlaygroundSupport
框架幸乒,import PlaygroundSupport
import UIKit
import PlaygroundSupport
let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
view.backgroundColor = .blue
PlaygroundPage.current.liveView = view
03-UIImageView
- 導(dǎo)入圖片資源文件
Logo.png
拖拽圖片文件到Resources
文件夾下
import UIKit
import PlaygroundSupport
let imageView = UIImageView(image: UIImage(named: "Logo"))
PlaygroundPage.current.liveView = imageView
04-UITableViewController
import UIKit
import PlaygroundSupport
let vc = UITableViewController()
vc.view.backgroundColor = .lightGray
// TODO: live設(shè)置為vc就可以了
PlaygroundPage.current.liveView = vc
05-注釋
- 單行注釋
// 單行注釋
- 多行注釋
/*
多行注釋
*/
- 多行注釋嵌套
/*
1
/* 多行注釋的嵌套 */
2
*/
06-Markup
- Playground的注釋支持
markup
語法(與markdown
相似) -
開啟markup
渲染效果:Editor -> Show Rendered Markup
-
關(guān)閉markup
渲染效果:Editor -> Show Raw Markup
注意: Markup只在Playground中有效
//: 開始markup
/*:
開始markup
*/
/*:
# 一級(jí)標(biāo)題
## 無序列表
- First Item
- Second Item
## 有序列表
1. First Item
2. Second Item
## 筆記
> This is a note.
---
## 圖片

## 鏈接
* [博客](http://www.reibang.com/u/bfcdf4a76abf)
## 粗體/斜體
這是**Bold**, 這是*Italic*
*/
07-常量
常量即在
初始化賦值之后
,就不允許
在進(jìn)行賦值
let age1 = 10
//age1 = 22 // 報(bào)錯(cuò): Cannot assign to value: 'age1' is a 'let' constant
let age2: Int
age2 = 20
func getAge() -> Int {
return 30
}
let age3 = getAge()
- 常量唇牧、變量在
未初始化之前
都不能使用
let age: Int
var height: Int
print(age) // error: constant 'age' used before being initialized
print(height) // error: variable 'height' used before being initialized
聲明常量后再單獨(dú)賦值,必須指定類型
let age
age = 20 // error: found an unexpected second identifier in constant declaration; is there an accidental break?
08-標(biāo)識(shí)符
- 標(biāo)識(shí)符(比如
常量名罕扎、變量名、函數(shù)名
)幾乎可以使用任何字符
- 標(biāo)識(shí)符
不能以數(shù)字開頭
,不能包含空白字符
丐重、制表符
腔召、箭頭
等特殊字符
func ????() {
print("666")
}
????()
let ?? = "ET"
var ?? = "milk"
09-常見數(shù)據(jù)類型
常用數(shù)據(jù)類型分為兩大類:
值類型
和引用類型
-
值類型
- 枚舉(enum)
- Optional
- 結(jié)構(gòu)體(struct)
- Bool、Int扮惦、Float臀蛛、Double、Character
- String崖蜜、Array浊仆、Dictionary、Set
- 枚舉(enum)
-
引用類型(reference type)
- 類(class)
- 整數(shù)類型: Int8豫领、Int16抡柿、 Int32、 Int64等恐、 UInt8洲劣、 UInt16备蚓、 UInt32、 UInt64
- 在
32bit
平臺(tái),Int等價(jià)于Int32
;在64bit
平臺(tái)囱稽,Int等價(jià)于Int64
- 整數(shù)的最值表示方式: UInt8. max郊尝、Int16.min
- 一般情況下,都是
直接使用Int
即可 - 浮點(diǎn)類型:
Float , 32位,精度只有6位
,Double , 64位,精度至少15位
10-字面量
// 布爾
let bool = true // 取反是false
// 字符串
let string = "哈哈哈"
// 字符(可存儲(chǔ)ASCII字符、Unicode字符)
let character: Character = "??"
// 整數(shù)
let intDecimal = 17 // 十進(jìn)制
let intBinary = 0b10001 // 二進(jìn)制
let intOctal = 0o21 // 八進(jìn)制
let intHexadecimal = 0x11 // 十六進(jìn)制
- 整數(shù)和浮點(diǎn)數(shù)可以
添加額外的零
或者添加下劃線
來增強(qiáng)可讀性
100_0000战惊、1_000_000.000_000_1流昏、000123.456
// 浮點(diǎn)數(shù)
let doubleDecimal = 125.0 // 十進(jìn)制,等價(jià)于1.25e2, 0.0125等價(jià)于1.25e-2
let doubleHexadecimal1 = 0xFp2 // 十六進(jìn)制,意味著15x2^2, 相當(dāng)于十進(jìn)制60.0
let doubleHexadecimal2 = 0xFp-2 // 十六進(jìn)制, 意味著15x2^-2, 相當(dāng)于十進(jìn)制的3.75
/* 以下都是表示12.1875
十進(jìn)制: 12.1875、1.21875e1
十六進(jìn)制: 0xC.3p0
*/
// 數(shù)組
let array = [1, 3, 5, 7, 9]
// 字典
// value值有整型,字符串類型,需指定類型為[String : Any], 否則報(bào)錯(cuò)Heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional
let dictionary: [String : Any] = ["age" : 18, "height" : 168, "weight": 120, "name" : "Liwx"]
11-類型轉(zhuǎn)換
注意:
Swift不支持不同類型的值進(jìn)行運(yùn)算
// 整數(shù)類型
let int1: UInt16 = 2_000
let int2: UInt8 = 1
let int3 = int1 + UInt16(int2)
// 整數(shù)样傍、浮點(diǎn)數(shù)轉(zhuǎn)換
let int = 3
let double = 0.14159
let pi = Double(int) + double
let intPi = Int(pi)
// 字面量可以直接相加,因?yàn)閿?shù)字字面量本身沒有明確的類型
let result = 3 + 0.14159
12-元組(Tuple)
元組(Tuple)
可以把多個(gè)值組合成一個(gè)復(fù)合值
横缔。元組內(nèi)的值可以是任意類型
,且不要求是相同類型衫哥。
-
不想接收值
,直接用下劃線_
- 可以使用
標(biāo)簽方式
// 元組
let http404Error = (404, "Not Found")
print("The status code is \(http404Error.0), status message is \(http404Error.1)")
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode), status message is \(statusMessage)")
// 不想接收值,直接用下劃線_
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// 使用標(biāo)簽方式
let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode), description is \(http200Status.description)")
iOS Swift 語法
底層原理
與內(nèi)存管理
分析 專題:【iOS Swift5語法】