Skip to content

Instantly share code, notes, and snippets.

View candostdagdeviren's full-sized avatar

Candost candostdagdeviren

View GitHub Profile
@candostdagdeviren
candostdagdeviren / Testability5.swift
Created December 3, 2018 12:39
Swift Post Testability Example Code
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
let isUnitTesting = ProcessInfo.processInfo.environment["IS_UNIT_TESTING"] == "1"
if isUnitTesting == false {
// Do UI-related setup, which can be skipped when testing
}
return true
}
@candostdagdeviren
candostdagdeviren / Testability4.swift
Created December 3, 2018 12:37
Swift Post Testability Example Code
class FileOpener {
let urlOpener: URLOpening
init(urlOpener: URLOpening = UIApplication.shared) {
self.urlOpener = urlOpener
}
func open(identifier: String) {
guard let url = URL(string: "iosappscheme://open?id=\(identifier)") else {
debugPrint("Failed to load URL")
return
@candostdagdeviren
candostdagdeviren / Testability3.swift
Created December 3, 2018 12:35
Swift Post Testability Example Code
protocol URLOpening {
func canOpenURL(_ url: URL) -> Bool
func open(_ url: URL,
options: [UIApplication.OpenExternalURLOptionsKey : Any],
completionHandler completion: ((Bool) -> Void)?)
}
extension UIApplication: URLOpening {}
@candostdagdeviren
candostdagdeviren / Testability2.swift
Created December 3, 2018 12:34
Swift Post Testability Example Code
class FileOpener {
let application: UIApplication
init(application: UIApplication = UIApplication.shared) {
self.application = application
}
func open(identifier: String) {
guard let url = URL(string: "iosappscheme://open?id=\(identifier)") else {
debugPrint("Failed to load URL")
return
@candostdagdeviren
candostdagdeviren / Testability1.swift
Created December 3, 2018 12:33
Swift Post Testability Example Code
class FileOpener {
func open(identifier: String) {
guard let url = URL(string: "iosappscheme://open?id=\(identifier)") else {
debugPrint("Failed to convert URL")
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
debugPrint("Failed to open URL")
@candostdagdeviren
candostdagdeviren / Concurrency3.swift
Last active November 28, 2018 10:09
Swift Post Concurrency Blog Post Code Example
class FetchRestaurantInventoryOperation: Operation {
var restaurantData: RestaurantData
var networkProvider: NetworkProvider
// 1
private var _executing = false {
willSet {
willChangeValue(forKey: "isExecuting")
}
didSet {
@candostdagdeviren
candostdagdeviren / Concurrency2.swift
Last active November 29, 2018 14:15
Swift Post Concurrency Blog Post Code Example
/* I put the first DispatchQueue method intentionally to indicate that we're on
the background queue. Just to support the explanation for beginner developers
This is not necessary in real-life, URLSession is thread-safe. Whenever you call URLSession
it'll start executing on background. */
DispatchQueue.global().async {
let url = URL(string: "https://theswiftpost.co/")
if let url = url {
DispatchQueue.main.sync {
showLoadingIndicator() // UI Operation; has to run on the main queue
}
@candostdagdeviren
candostdagdeviren / Concurrency1.swift
Last active November 29, 2018 14:09
Swift Post Concurrency Blog Post Code Example
let url = URL(string: "https://theswiftpost.co/")
if let url = url {
showLoadingIndicator() // UI Operation; has to run on the main queue
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
showErrorAlert(error) // UI operation; has to run on the main queue!!
} else if let data = data {
debugPrint(usableData) //JSONSerialization
}
debugPrint(“Completed”)
@candostdagdeviren
candostdagdeviren / ViewLifecycle.swift
Created November 18, 2018 11:45
View Lifecycle - NS for iOS Devs
// Adding a view controller to another
let childViewController = UIViewController()
let parentViewController = UIViewController()
parentViewController.addChild(childViewController)
parentViewController.view.addSubview(childViewController.view)
// setup auto-layout constraints for childViewController.view
childViewController.didMove(toParent: parentViewController)
// Removing the child view controller from parent
childViewController.willMove(toParent: nil)
@candostdagdeviren
candostdagdeviren / ViewRenderable.swift
Last active August 9, 2018 08:10
Custom View Implementation for iOS
import UIKit
protocol ViewCustomized {
func createView<T>(_ viewClass: T.Type, parentView: UIView) -> T where T: UIView, T: ViewRenderable
}
protocol ViewRenderable {
func render()
}