定義類
import UIKit
class Student: NSObject {
//構(gòu)造函數(shù)
init(name:String,age:Int) {
self.name=name
self.age=age
}
}
定義變量
//定義變量不賦值
var name:NSString?
//定義變量賦值
var age=25
var height:Int = 175
//定義常量
let sex = "男"
let homedown:NSString="湖北省宜城市"
定義方法
func introduceYourSelf() -> Void {
NSLog("%@", "我的名字叫"+name!+";我的年齡是"+String(age))
}
func testFunc(str:String,nextStr:String,number:Int) -> String {
let strRestul=str+nextStr+String(number);
return strRestul;
}
創(chuàng)建對象
let stu=Student.init(name: "stonemover", age: 22)//聲明對象
stu.height=172//設(shè)置變量值
stu.introduceYourSelf()//調(diào)用方法
let result=stu.testFunc(str: "第一個字符", nextStr: "第二個字符", number: 10)//調(diào)用方法傳入?yún)?shù),返回結(jié)果
print(result)//打印結(jié)果
枚舉 enum的定義
enum BTToastLocationType {
case BTToastLocationCenter
case BTToastLocationBottom
}
enum BTToastLocationType {
case BTToastLocationCenter,BTToastLocationBottom
}
as 關(guān)鍵字
as關(guān)鍵字是用來做類型強(qiáng)制轉(zhuǎn)換
let cell:MainTableViewCell=tableView.dequeueReusableCell(withIdentifier: "mainCell") as! MainTableViewCell
! 與? 特殊符號
self.label!
明確該變量不是nil一定有值
例如:self.label!.text="test",就一定會執(zhí)行
self.label?
不確定該變量是否有值,只在self.label有值的時候進(jìn)行相關(guān)方法的調(diào)用.
例如:self.label?.text,當(dāng)self.label有值的時候執(zhí)行,當(dāng)沒有值的時候就不執(zhí)行
mark標(biāo)記
//MARK: 外部調(diào)用方法
圖片.png
快速使用圖片資源
直接打出圖片資源的名稱即可自動轉(zhuǎn)換為UIImage對象
數(shù)組反轉(zhuǎn)
self.dataPerson = self.dataPerson.reversed()
讓閉包作為類的一個屬性
typealias chooseBlock = (_ sendString: String) -> Void
var successBlock : ((_ sendString : String) ->Void)?
class ChoosePageController: WMPageController {
var blockSuccess : chooseBlock?
override func viewDidLoad() {
self.blockSuccess?("123")
}
}
讓閉包作為一個參數(shù)傳入
func test(closure: ( sendString : String) -> Void) {
closure("")
}
監(jiān)聽?wèi)?yīng)用掛起
NotificationCenter.default.addObserver(self, selector:#selector(willResionActive),name: UIApplication.willEnterForegroundNotification,object: nil)
返回多個值的情況
//MARK:屏幕旋轉(zhuǎn)
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if BTUtils.getCurrentVc()?.isKind(of: VideoDetailVC.self) ?? false {
let vc = BTUtils.getCurrentVc() as! VideoDetailVC
if vc.isOnlyVoice {
return .portrait
}
return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.landscapeRight.rawValue | UIInterfaceOrientationMask.portrait.rawValue)
}
return UIInterfaceOrientationMask.portrait
}
定義創(chuàng)建數(shù)組
//先定義后創(chuàng)建
var array : Array<FileModel>!
//直接創(chuàng)建
var array = [FileModel]()
循環(huán)同時獲取下標(biāo)和值引用
for (index,var dict) in dictArray!.enumerated() {
let str = dict["pwd"] as! String
dict.updateValue(AppHelp.help.decode(str), forKey: "pwd")
}