Skip to content

Instantly share code, notes, and snippets.

@ahmedk92
ahmedk92 / ViewController.swift
Created November 20, 2019 17:47
UINavigationBar Subview
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
@ahmedk92
ahmedk92 / WKWebView+TypeSafeJSEvaluate.swift
Created September 15, 2019 14:49
Extension methods for type-safely evaluating JS in a WKWebView
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
}
@ahmedk92
ahmedk92 / UIView+Traverse.swift
Last active August 8, 2019 13:02
Exhaustively traverse the subview hierarchy of a given UIView while maintaining type safety.
extension UIView {
func traverse<T>(visitor: (T) -> ()) {
for subview in subviews {
if let tView = subview as? T {
visitor(tView)
}
subview.traverse(visitor: visitor)
}
}
@ahmedk92
ahmedk92 / PageViewController.swift
Created July 18, 2019 16:10
UIPageViewController setViewControllers not calling dataSource methods fix
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)
@ahmedk92
ahmedk92 / commonInit.m
Last active May 22, 2019 12:52 — forked from aaronshekey/commonInit.m
Common initialization methods for a custom view in Objective C
- (void)commonInit {
// Do stuff.
}
- (instancetype)init {
if (self = [super init]) {
[self commonInit];
}
return self;
@ahmedk92
ahmedk92 / Demo.swift
Last active March 20, 2019 08:25
Auto-releasing Sequence Map function
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) })
@ahmedk92
ahmedk92 / EagerLogicalAnd.Swift
Created January 11, 2019 17:29
Eager Logical AND
infix operator &&&: LogicalConjunctionPrecedence
extension Bool {
static func &&& (left: Bool, right: Bool) -> Bool {
return left && right
}
}
@ahmedk92
ahmedk92 / RetainCycleDemo.swift
Created July 5, 2018 18:15
Swift Retain Cycle Demo
// =====================================
// NO RETAIN CYCLE
class ViewController: UIViewController {
private var dataSource = DataSource() // Strong reference. But in just one way; no cycle.
}
class DataSource: NSObject, UITableViewDataSource {
}
@ahmedk92
ahmedk92 / main.swift
Created May 12, 2018 15:26
Set default app language
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()
}
@ahmedk92
ahmedk92 / UrlQueryEscaping.swift
Created April 8, 2018 14:25
Url Query Escaping
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)!