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
// 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 |
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
//: Playground - noun: a place where people can play | |
import UIKit | |
class Customer { | |
let udid: String | |
let name: String | |
init(udid: String, name: 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
// 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 |
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
// 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 | |
*/ |
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
// 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 |
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
// 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 |
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
// 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 { |
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 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] | |
*/ |
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
// MARK: - Autorelease pool | |
// Reduce device memory usage | |
for i in 0...1000 { | |
autoreleasepool { | |
let image = UIImage(named: "\(i)") | |
print("image: \(image?.size)") | |
} | |
} |
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 | |
protocol TaskListModelInput { | |
var card: CardDataModel! { get set } | |
func upload(image: Data, for task: TaskDataType) | |
} |