This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
window = UIWindow(frame: UIScreen.main.bounds) | |
window?.makeKeyAndVisible() | |
window?.rootViewController = UINavigationController(rootViewController: ViewController()) | |
return true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
title = "ViewController" | |
view.backgroundColor = UIColor.brown | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HTMLElement { | |
let name: String | |
let text: String | |
// 參考self的closure造成 記憶體無法釋放的retain cycle | |
lazy var asHTML: () -> String = { | |
return "<\(self.name)>\(self.text)</\(self.name)>" | |
} | |
init(name: String, text: String) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HTMLElement { | |
let name: String | |
let text: String | |
lazy var asHTML: () -> String = { | |
[weak self] in //以弱參考修飾捕獲的self使參考計數不計 | |
return "<\(self?.name)>\(self?.text)</\(self?.name)>",// 因為weak是弱參考屬optional所以在鏈上需要加入?使用 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazy var asHTML: () -> String = { | |
[weak self] in | |
guard let this = self else { return "" } | |
return "<\(this.name)>\(this.text)</\(this.name)>" | |
} | |
//換個角度來看self,先用guard let 安全的解包 optional self,在來使用this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Two object retain cycle pattern | |
class Person: NSObject { | |
var name: String | |
var deparment: Department? | |
init(name:String) { | |
self.name = name | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let redView = UIView() | |
redView.backgroundColor = .red | |
view.addSubview(redView) | |
redView.translatesAutoresizingMaskIntoConstraints = false | |
redView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true | |
redView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40).isActive = true | |
redView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true | |
redView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100).isActive = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let redView = UIView() | |
redView.backgroundColor = .red | |
view.addSubview(redView) | |
redView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
redView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40), | |
redView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40), | |
redView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40), | |
redView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SingletonClass { | |
static let shared = SingletonClass() | |
private init() { } | |
func someMethod() { } | |
let constantProperty = "" | |
var variableProperty = "" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SingletonClass.shared.constantProperty | |
SingletonClass.shared.someMethod() | |
SingletonClass.shared.variableProperty |
OlderNewer