1.寫(xiě)法對(duì)比
oc
self.window = [UIWindow alloc]initWithFrame:UIScreen.main.bounds];
[self.view addSubview:子視圖]
swift
self.window = UIWindow(frame:UIScreen.main.bounds);
let vc = ViewController();
??let nav = UINavigationController(rootViewController:vc)
self.window =類(lèi)名(屬性:屬性值);
self.view.addSubview(子視圖);//之前的方法調(diào)用由中括號(hào)形式變?yōu)閷傩孕问?/p>
button
override func viewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? self.title = "測(cè)試";
? ? ? ? let but = UIButton();
? //上左下右-----控制圖片和文字的位置
? ? ? ? bu.imageEdgeInsets=UIEdgeInsetsMake(-20,20,0,0)
? ? ? ? bu.titleEdgeInsets=UIEdgeInsetsMake(30,-80,-20,0)
? ? ? ? but.frame = CGRect.init(x:20,y:100,width:80,height:40);
? ? ? ? but.backgroundColor = UIColor.yellow;
? ? ? ? but.setTitle("but", for: UIControlState.normal);
? ? ? ? but.addTarget(self, action:#selector(buttonOneClicked), for: UIControlEvents.touchUpInside);
? ? ? ? self.view.addSubview(but);
self.view.addSubview(self.but);
? ? ? ? self.view.backgroundColor = UIColor.red;
? ? ? ? // Do any additional setup after loading the view, typically from a nib.
? ? }
? ? @objc func buttonOneClicked(){
? ? ? ? print("123");
? ? ? ? NSLog("123");
? ? ? ? let v2 = ViewController2();
? ? ? ? self.navigationController?.pushViewController(v2, animated: true);
? ? }
懶加載
好處:將對(duì)象的創(chuàng)建延遲到了需要對(duì)象的時(shí)候,這樣減少了內(nèi)存開(kāi)銷(xiāo)。另外這樣也將創(chuàng)建對(duì)象凤藏、相關(guān)屬性?xún)?nèi)聚在一個(gè)代碼塊內(nèi),降低了其他模塊的復(fù)雜度阎肝。
使用:在swift中,實(shí)現(xiàn)懶加載需要lazy和var關(guān)鍵字肮街,如下:
lazy var but:UIButton = {
? ? ? ? ()->UIButton in
? ? ? ? let tempBut = UIButton();
? ? ? ? tempBut.backgroundColor = UIColor.yellow;
? ? ? ? tempBut.frame = CGRect.init(x:20,y:100,width:80,height:40);
? ? ? ? tempBut.setTitle("but", for: UIControlState.normal);
? ? ? ? tempBut.addTarget(self, action:#selector(buttonOneClicked), for: UIControlEvents.touchUpInside);
? ? ? ? return tempBut;
? ? }();
總結(jié):swift實(shí)現(xiàn)懶加載必須使用var關(guān)鍵字來(lái)定義延時(shí)加載的屬性,而不可以使用let關(guān)鍵字风题,因?yàn)閘et關(guān)鍵字定義的是常量,而常量必須在實(shí)例創(chuàng)建時(shí)賦值嫉父。另外swift定義懶加載的規(guī)則沛硅,即"后面通過(guò)等號(hào)賦值一個(gè)閉包,閉包后面必須加上()"绕辖。
也可以這樣:
lazy var butt:UIButton = {
? ? ? ? let tempBut = UIButton()
? ? ? ? tempBut.backgroundColor = UIColor.yellow
? ? ? ? tempBut.frame = CGRect.init(x:20,y:150,width:80,height:40)
? ? ? ? tempBut.setTitle("but", for: UIControlState.normal)
? ? ? ? tempBut.addTarget(self, action:#selector(buttonOneClicked), for: UIControlEvents.touchUpInside)
? ? ? ? return tempBut
? ? }();
UITableView
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
? ? let arrStr = ["1","2","3","12","23","33",];
? ? func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
? ? ? ? NSLog(arrStr[indexPath.row], indexPath.row);
? ? }
? ? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
? ? ? ? return arrStr.count;
? ? }
? ? func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
? ? ? ? let cell = (self.myTableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)) as UITableViewCell
? ? ? ? cell.textLabel?.text = arrStr[indexPath.row]
? ? ? ? return cell
? ? }
? ? override func viewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? self.title = "測(cè)試";
? ? ? ? self.view.addSubview(self.myTableView);
? ? ? ? ? self.view.backgroundColor = UIColor.red;
? ? }
? ? lazy var myTableView:UITableView = {
? ? ? ? let tempTableV = UITableView(frame:self.view.frame);
? ? ? ? tempTableV.delegate = self;
? ? ? ? tempTableV.dataSource = self;
? ? ? ? tempTableV.backgroundColor = UIColor.blue;
? ? ? ? //注冊(cè)UITableView摇肌,cellID為重復(fù)使用cell的Identifier
? ? ? ? tempTableV.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
? ? ? ? return tempTableV;
? ? }();
? ? override func didReceiveMemoryWarning() {
? ? ? ? super.didReceiveMemoryWarning()
? ? ? ? // Dispose of any resources that can be recreated.
? ? }
}