class LoginViewController: UIViewController { //Class 'LoginViewController' has no initializers private var loginButton: UIButton override func viewDidLoad() { super.viewDidLoad() loginButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 36)) view.addSubview(loginButton) } }
class LoginFragment : Fragment() { private lateinit var loginButton: Button override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) loginButton = view.findViewById(R.id.login_button) } }
class LoginViewController: UIViewController { @Lateinit private var loginButton: UIButton override func viewDidLoad() { super.viewDidLoad() loginButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 36)) view.addSubview(loginButton) } }
@propertyWrapper struct Lateinit<T> { private var _value: T? var wrappedValue: T { get { guard let value = _value else { fatalError("Lateinit property has not been initialized") } return value } set { _value = newValue } } }
acceptButton.setBackgroundColor(SplashGDPRColors.acceptButton.value, for: .normal) acceptButton.setTitleColor(.white, for: .normal) acceptButton.setTitle(String(PG_gdprContinue), for: .normal) acceptButton.clipsToBounds = true acceptButton.layer.cornerRadius = 3 acceptButton.isEnabled = true
withObject(acceptButton) { $0.setBackgroundColor(SplashGDPRColors.acceptButton.value, for: .normal) $0.setTitleColor(.white, for: .normal) $0.setTitle(String(PG_gdprContinue), for: .normal) $0.clipsToBounds = true $0.layer.cornerRadius = 3 $0.isEnabled = true }
let row = itemArray.firstIndex { $0.type == .tool } if let row = row { let indexPath = IndexPath(row: row, section: 0) if let toolCell = tableView.cellForRow(at: indexPath) as? MainPageToolCell { toolCell.refreshDraftsEntrance() } }
itemArray.firstIndex { $0.type == .tool }?.let { //$0: Int IndexPath(row: $0, section: 0) }.let { //$0: IndexPath tableView.cellForRow(at: $0) as? MainPageToolCell }?.also { //$0: MainPageToolCell $0.refreshDraftsEntrance() }
Function | Object reference | Return value | Is extension function |
---|---|---|---|
let | it | Lambda result | YES |
run | this | Lambda result | YES |
with | this | Lambda result | NO |
apply | this | Context object | YES |
also | it | Context object | YES |
val str = "Hello" // this str.run { println("The string's length: $length") //println("The string's length: ${this.length}") // does the same } // it str.let { println("The string's length is ${it.length}") }
//run val price = Book().run { name = "Scope Functions" price = 400 price }
//let val price = Book().let { it.name = "Scope Functions" it.price = 400 it.price }
//with val book = Book() val privce = with(book) { name = "Scope Functions" price = 400 price }
//apply val book = Book().apply { name = "Scope Functions" price = 400 } //also val book = Book().also { it.name = "Scope Functions" it.price = 400 }
//let let price: Int = Book().let { $0.name = "Scope Functions" $0.price = 400 $0.price }
//also let bool = Book().also { $0.name = "Scope Functions" $0.price = 400 }
//withStruct let book: Book = ... let price: Int = with(book) { book.checked = true return book.price }
@inlinable public func withStruct<T: Any, V>(_ value: T, _ block: (inout T) throws -> V) rethrows -> V { var copy = value return try block(©) }
@inlinable @discardableResult public func withObject<T: AnyObject, V>(_ value: T, _ block: (T) throws -> V) rethrows -> V { return try block(value) }
extension ScopeFunction where Self: Any { @inlinable @discardableResult public func `let`<T>(_ block: (inout Self) throws -> T) rethrows -> T { var copy = self return try block(©) } @inlinable public func also(_ block: (inout Self) throws -> Void) rethrows -> Self { var copy = self try block(©) return copy } }
let dic = [String: String]() //dic.let { it -> [String: String] in //dic.also { it -> [String: String] in withStruct(dic) { it -> [String: String] in it["Name"] = "Allen" return it }.also { print($0) } print("\(dic)")
var dic = [String: String]() //assign the return value! dic = withStruct(dic) { it -> [String: String] in it["Name"] = "Allen" return it }.also { print($0) } print("\(dic)")
Function | Return value | Value type | Reference type |
---|---|---|---|
let | Lambda result | v | |
also | Context object | v | |
withStruct | Lambda result | v | |
withObject | Lambda result | v | |
takeIf | Context object | v | |