Skip to content

Instantly share code, notes, and snippets.

Candost Dagdeviren candostdagdeviren

View GitHub Profile
@candostdagdeviren
candostdagdeviren / Testability5.swift
Created Dec 3, 2018
Swift Post Testability Example Code
View Testability5.swift
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 Dec 3, 2018
Swift Post Testability Example Code
View Testability4.swift
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 Dec 3, 2018
Swift Post Testability Example Code
View Testability3.swift
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 Dec 3, 2018
Swift Post Testability Example Code
View Testability2.swift
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 Dec 3, 2018
Swift Post Testability Example Code
View Testability1.swift
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 Nov 28, 2018
Swift Post Concurrency Blog Post Code Example
View Concurrency3.swift
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 Nov 29, 2018
Swift Post Concurrency Blog Post Code Example
View Concurrency2.swift
/* 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 Nov 29, 2018
Swift Post Concurrency Blog Post Code Example
View Concurrency1.swift
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 Nov 18, 2018
View Lifecycle - NS for iOS Devs
View ViewLifecycle.swift
// 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 Aug 9, 2018
Custom View Implementation for iOS
View ViewRenderable.swift
import UIKit
protocol ViewCustomized {
func createView<T>(_ viewClass: T.Type, parentView: UIView) -> T where T: UIView, T: ViewRenderable
}
protocol ViewRenderable {
func render()
}
You can’t perform that action at this time.