Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

🎯
Focusing
View GitHub Profile
lazy var loginCompletion: (Bool) -> Void = { [weak self] success in
if let `self` = self, success {
self.navigationController?.popToRootViewController(animated: true)
}
}
/*
[weak self] is closure capture list. It can be added all parameters what you need.
i.e. [weak self, unowned delegate]
*/
// MARK: - Strong reference cycle for closures
class ViewController: UIViewController {
lazy var loginCompletion: (Bool) -> Void = { success in // <- place where we has been mistaken. It must be resolved using capture list with 'unowned' reference
if success {
self.navigationController?.popToRootViewController(animated: true)
}
}
deinit {
// MARK: - Strong reference cycle
class Owner {
let udid: String
let iin: String
let name: String
var business: Business!
init(udid: String, iin: String, name: String) {
self.udid = udid
// MARK: - unowned reference. Prevent strong reference cycle
class Owner {
let udid: String
let iin: String
let name: String
var business: Business!
init(udid: String, iin: String, name: String) {
self.udid = udid
// MARK: - weak reference
weak var weakCustomer1stRef: Customer? = nil
weak var weakCustomer2stRef: Customer? = nil
weakCustomer1stRef = Customer(udid: "weak 1", name: "Jacob")
/*
print: weak 1 initialized
print: weak 1 deinitialized
*/
// MARK: - strong reference counting
var customer1stRef: Customer? = nil
var customer2ndRef: Customer? = nil
customer1stRef = Customer(udid: "1st", name: "Maxim")
/*
print: 1st initialized
*/
customer2ndRef = customer1stRef
customer1stRef = nil
//: Playground - noun: a place where people can play
import UIKit
class Customer {
let udid: String
let name: String
init(udid: String, name: String) {
// MARK: API availability
var color: UIColor?
if #available(iOS 11, *) {
// Use iOS 11 APIs on iOS
color = UIColor(named: "green_color_from_asset_folder")
} else {
// Fall back to earlier iOS APIs
color = UIColor.green
// MARK: guard
typealias JSON = [String: Any?]
struct Account {
let udid: String
let token: String
}
extension Account {
// MARK: Continue, break, throw
let numbers: [Int] = [1, 2, 3, 4, 5, 10, 11, 20]
// MARK: continue
var evenSum = 0
for number in numbers {
if number % 2 != 0 { continue }
evenSum += number