Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

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