Skip to content

Instantly share code, notes, and snippets.

import UIKit
let appDelegateClass: AnyClass = NSClassFromString("TestingAppDelegate") ?? AppDelegate.self
UIApplicationMain(CommandLine.argc,
CommandLine.unsafeArgv,
nil, NSStringFromClass(appDelegateClass))
import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("--> Launching from \(type(of: self))")
window = UIWindow(frame: UIScreen.main.bounds)
import UIKit
@objc(TesingAppDelegate)
class TestingAppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("--> Launching from \(type(of: self))")
window = UIWindow(frame: UIScreen.main.bounds)
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("--> Launching from \(type(of: self))")
window = UIWindow(frame: UIScreen.main.bounds)
// Without Dependency Injection
class Company {
var name: String
// `ceo` is dependency here, but Company initializes the ceo object,
// `Company` knows how to initilize the ceo
var ceo: Employee = Employee(name: "Steve", salary: 12344)
init(name: String) {
self.name = name
}
@boudhayan
boudhayan / protocol.swift
Created April 8, 2022 11:06
Protocol and Delegate
import Foundation
//MARK: Protocol
/*
protocol is a set of rules, whichever class adopts the protocol, needs to implement the variables and functions declared inside that protocol.
For the below protocol Car, it has two requirements, model and topSpeed.
So if any class or struct adopts this protocol, needs to implement model and topSpeed.
*/
protocol Car {
protocol AEMValue { }
extension String: AEMValue { }
extension Int: AEMValue { }
struct AEMDictionary<T> {
private let container: [String: T]
init(_ dict: [String: T]) {
container = dict
//MARK: Custom Implmentation of map, filter, reduce, flatMap
public extension Array {
//map
func map2<T>(_ transform: (Element) -> T) -> [T] {
var mapped = [T]()
mapped.reserveCapacity(count)
for element in self {
mapped.append(transform(element))
}
return mapped
struct Employee {
let name: String
private var _employer: Company
var employer: Company {
mutating get {
if !isKnownUniquelyReferenced(&_employer) {
_employer = _employer.copy() as! Company
}
return _employer
class Company: NSCopying {
var name: String = "Confidential"
func copy(with zone: NSZone? = nil) -> Any {
let copy = Company()
copy.name = name
return copy
}
deinit {