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 { | |
private lazy var button: UIButton = { | |
let button = UIButton(frame: .zero) | |
button.setTitle("Hi", for: .normal) | |
button.backgroundColor = .red | |
button.setTitleColor(.white, for: .normal) | |
return button |
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
extension WKWebView { | |
enum EvaluateJavaScriptError: String, Error { | |
case typeMismatchError | |
} | |
func evaluateJavaScript(_ javaScriptString: String, completionHandler: ((Result<String, Error>) -> Void)? = nil) { | |
evaluateJavaScript(javaScriptString) { (result, error) in | |
guard let result = result else { | |
completionHandler?(.failure(error!)) | |
return | |
} |
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
extension UIView { | |
func traverse<T>(visitor: (T) -> ()) { | |
for subview in subviews { | |
if let tView = subview as? T { | |
visitor(tView) | |
} | |
subview.traverse(visitor: visitor) | |
} | |
} |
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 PageViewController: UIPageViewController { | |
override func setViewControllers(_ viewControllers: [UIViewController]?, direction: UIPageViewController.NavigationDirection, animated: Bool, completion: ((Bool) -> Void)? = nil) { | |
super.setViewControllers(viewControllers, direction: direction, animated: animated) { (isFinished) in | |
if isFinished && animated { | |
DispatchQueue.main.async { | |
super.setViewControllers(viewControllers, direction: direction, animated: false, completion: nil) | |
} | |
} | |
completion?(isFinished) |
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
- (void)commonInit { | |
// Do stuff. | |
} | |
- (instancetype)init { | |
if (self = [super init]) { | |
[self commonInit]; | |
} | |
return self; |
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 range = 0..<5000 | |
// Hogs up to GBs of memory till total evaluation. | |
let items: [Item] = Array(range).map({ Item(id: $0) }) | |
// Frees memory soon after every evaluation. | |
let items: [Item] = Array(range).autoreleasingMap({ Item(id: $0) }) |
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
infix operator &&&: LogicalConjunctionPrecedence | |
extension Bool { | |
static func &&& (left: Bool, right: Bool) -> Bool { | |
return left && right | |
} | |
} |
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
// ===================================== | |
// NO RETAIN CYCLE | |
class ViewController: UIViewController { | |
private var dataSource = DataSource() // Strong reference. But in just one way; no cycle. | |
} | |
class DataSource: NSObject, UITableViewDataSource { | |
} |
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 MyApplication: UIApplication { | |
override init() { | |
let notFirstOpenKey = "notFirstOpen" | |
let notFirstOpen = UserDefaults.standard.bool(forKey: notFirstOpenKey) | |
if notFirstOpen == false { | |
UserDefaults.standard.set(["ar"], forKey: "AppleLanguages") | |
UserDefaults.standard.set(true, forKey: notFirstOpenKey) | |
} | |
super.init() | |
} |
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 queryParams = "{query}{&page, per_page, sort, order}" | |
let urlString = "https://api.github.com/search/repositories?q=" + queryParams.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! | |
let url = URL(string: urlString)! |