swift.png
前言:其實(shí)很早就開始接觸swift桑逝,當(dāng)時(shí)只是覺(jué)得這個(gè)語(yǔ)法怎么有點(diǎn)奇特,也讓代碼簡(jiǎn)單了些山宾。 但是由于各種各樣的原因至扰,好久沒(méi)接觸了,當(dāng)初學(xué)得基礎(chǔ)語(yǔ)法也拋在云端了资锰。 今天決定拾起swift敢课,從40個(gè)小tips開始(很早以前下載的demo集,沒(méi)有作者了,只能說(shuō)聲感謝了前輩) --這個(gè)系列文章不是教你寫demo的直秆,只是我寫demo中覺(jué)得自己不熟悉的知識(shí)點(diǎn)保存下來(lái)濒募,方便以后查找(這是我風(fēng)格了,太健忘了)
// MARK:- 01 讓代碼穿上衣服
#不知道剛從OC過(guò)渡到Swift的朋友圾结,第一次看見(jiàn)一長(zhǎng)串的Swift代碼時(shí)瑰剃,是不是覺(jué)得每個(gè)方法就像沒(méi)穿衣服一樣,赤裸裸的筝野,也什么邏輯
我們可以這樣做—比如tableView的代理和數(shù)據(jù)源方法晌姚,我們可以單獨(dú)寫個(gè)分類出來(lái)
// MARK:- tableView -- dataSource
extension ViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
//返回高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
// MARK:- 02 打印拼接
#這里只說(shuō)一種情況—我學(xué)習(xí)Swift的策略是,了解基礎(chǔ)語(yǔ)法后歇竟,就模仿簡(jiǎn)單demo的實(shí)現(xiàn)挥唠,有不懂的就直接百度或者是Google
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//打印得注意了
print("點(diǎn)擊了\(indexPath.row)行cell")
}
// MARK:- 03 tableViewCell的創(chuàng)建
#代碼創(chuàng)建,并且沒(méi)有注冊(cè)cell的情況下焕议,用
dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell?
# 如果已經(jīng)注冊(cè)了宝磨,或者用的xib,就使用
dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell
// MARK:- 04 空合運(yùn)算符
#簡(jiǎn)介:
空合運(yùn)算符(a ?? b)將對(duì)可選類型a進(jìn)行空判斷盅安,如果a包含一個(gè)值就進(jìn)行解封懊烤,否則就返回一個(gè)默認(rèn)值b.這個(gè)運(yùn)算符有兩個(gè)條件:
表達(dá)式a必須是Optional類型
默認(rèn)值b的類型必須要和a存儲(chǔ)值的類型保持一致
#運(yùn)用:
我們創(chuàng)建cell可以直接這樣寫和下面是一樣的效果
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) ?? UITableViewCell(style: .subtitle, reuseIdentifier: reuseIdentifier)
/*****************************************************/
var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
if cell == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
// MARK:- 05 導(dǎo)入字體的步驟
1.下載ttf文件,加入項(xiàng)目中
2.在info.plist中宽堆,添加一個(gè)字段:Fonts provided by application—將字體名字添加到item里面
3.然后就可以通過(guò)名字使用了
// MARK:- 06 顏色設(shè)置快捷鍵
button.backgroundColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
#這上面這個(gè)是從代碼中直接復(fù)制過(guò)來(lái)的腌紧,設(shè)置顏色時(shí)直接
button.backgroundColor = colcor 按回車有彩蛋哦
// MARK:- 07 guard守衛(wèi)語(yǔ)法
#在Tips-03小項(xiàng)目中,我們從本地加載一個(gè)文件
let path = Bundle.main.path(forResource: "emoji zone", ofType: "mp4”)
#我們加載了這個(gè)文件畜隶,可以進(jìn)行一個(gè)判斷壁肋,我們是否將文件名寫正確呢,先看以前的做法 if 判斷
if path == nil {
print("沒(méi)有該文件籽慢!")
return
}
#看著也很簡(jiǎn)單是吧浸遗,為什么還要用guard呢(這個(gè)自己查查吧)
guard path != nil else {
print("沒(méi)有該文件!")
return
}
#判斷path為不為空,不為空箱亿,執(zhí)行g(shù)uard下面的代碼跛锌,為空,就執(zhí)行else中的代碼
// MARK:- 08 用系統(tǒng)字體為你的應(yīng)用帶來(lái)改變
- 看下面這張圖形中的圖片中的字體是不是感覺(jué)比較炫酷
1.png
- 其實(shí)這只是設(shè)置了一下系統(tǒng)自帶字體而已
videoTitle.font = UIFont(name: "Zapfino", size: 24)
貼個(gè)網(wǎng)站届惋,iOS 所有自帶字體
iOS字體
// MARK:- 09 遍歷數(shù)組enumerated()
#類似于OC Block塊遍歷
let arr = ["tmac","kobe","kg"]
for (index,value) in arr.enumerated() {
print(index)
print(value)
}