Skip to content

Instantly share code, notes, and snippets.

View aainaj's full-sized avatar

Aaina Jain aainaj

  • GoJek Tech
  • Bangalore, India
View GitHub Profile
@aainaj
aainaj / FeaturedTopicsConcurrentAccessFailure.swift
Created June 2, 2020 04:07
Featured Topics Concurrent Access Failure
func testFeaturedTopics() {
let count = 200
for index in 0..<count {
FeaturedTopics.shared[String(index)] = index
}
// The dispatch queue executes the submitted blocks in parallel and waits for all iterations to complete before returning
DispatchQueue.concurrentPerform(iterations: count) { (index) in
if let indexNumber = FeaturedTopics.shared[String(index)] as? Int {
print(indexNumber)
@aainaj
aainaj / FeaturedTopics.swift
Created June 2, 2020 03:40
Class using Singleton
final class FeaturedTopics {
private var topics: [String: Any] = [:]
static let shared = FeaturedTopics()
private init() {}
subscript(key: String) -> Any? {
get {
return topics[key]
@aainaj
aainaj / UIViewBuild.swift
Last active April 3, 2020 07:37
UIView Builder Pattern
extension UIView {
@inline(__always) static func build<T>(applyAttributes: ((T) -> Void)? = nil) -> T where T: UIView {
let uiComponent = T(frame: .zero)
uiComponent.translatesAutoresizingMaskIntoConstraints = false
applyAttributes?(uiComponent)
return uiComponent
}
}
let label: UILabel = {
@aainaj
aainaj / UILabelBuild.swift
Created April 2, 2020 14:11
UILabel builder pattern
extension UILabel {
static func build(block: ((UILabel) -> Void)) -> UILabel {
let label = UILabel(frame: .zero)
label.translatesAutoresizingMaskIntoConstraints = false
block(label)
return label
}
}
@aainaj
aainaj / URLBuilderPattern.swift
Last active April 3, 2020 03:28
URL Builder Pattern
protocol ScopeFunc {}
extension ScopeFunc {
@inline(__always) func apply(block: (Self) -> ()) -> Self {
block(self)
return self
}
}
class URLBuilder: ScopeFunc {
private var components = URLComponents()
@aainaj
aainaj / BuilderPatternWithOptionalsUsingStruct.swift
Created April 2, 2020 13:57
BuilderPattern for creating multiple type of objects using struct for model
struct OptionDetailDataStruct: Equatable {
let isExpired: Bool
let expiryDate: String?
let cardNumber: String?
let formattedNumber: String?
let phoneNumber: String?
}
class OptionDetailDataBuilderStruct {
private var isExpired: Bool = false
@aainaj
aainaj / BuilderPatternWithOptionals.swift
Created April 2, 2020 03:57
BuilderPattern for creating multiple type of objects
class OptionDetailData: Equatable {
var isExpired: Bool = false
var expiryDate: String?
var cardNumber: String?
var formattedNumber: String?
var phoneNumber: String?
static func == (lhs: OptionDetailData, rhs: OptionDetailData) -> Bool {
return true
}
@aainaj
aainaj / object-pool.swift
Created March 11, 2020 12:37
Example to show object pool design pattern
enum PaymentMethod {
case creditCard
case debitCard
case wallet
case cash
}
protocol PaymentPresenterStrategy {}
protocol MethodsPresenterResolvable {
@aainaj
aainaj / prototype-pattern.swift
Last active March 10, 2020 10:32
Example to show prototype design pattern
import Foundation
protocol Cloneable {
func copy() -> Any
}
class Message: Cloneable {
var to: String
var subject: String
@aainaj
aainaj / DispatchQueueAsyncTaskRetainCycle.swift
Last active January 13, 2020 17:04
self in dispatch queue doesn't cause retain cycle
import UIKit
class ListViewController: UIViewController {
let label = UILabel(frame: CGRect(x: 100, y: 200, width: 300, height: 50))
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white