Skip to content

Instantly share code, notes, and snippets.

View dmhts's full-sized avatar
🇺🇦
https://stand-with-ukraine.pp.ua/

Dima Hutsuliak dmhts

🇺🇦
https://stand-with-ukraine.pp.ua/
View GitHub Profile
@dmhts
dmhts / gist:9376504
Created March 5, 2014 21:02
Bulletproof singleton
/**
* Bulletproof singletone constructor. Can be used with or
* without new, can be called from whatever (object method,
* standalone function). Works fine in strict mode
*/
var Singletone = (function () {
var instance;
return function Construct_singletone () {
if (instance) {
// - MARK: - Swift
let startTime = CACurrentMediaTime()
// Code to test
print("Runtime: \((CACurrentMediaTime() - startTime) * 1000) ms")
// MARK: - Obj-C
// Measuring runtime of the particular part of code
CFTimeInterval startTime = CACurrentMediaTime();
@dmhts
dmhts / gist:29afa444f01bde3a6889e65e3463fe7e
Created November 11, 2016 09:34
Obj-C thread-safe singleton
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
// MARK: Delegate approach
import UIKit
protocol ExampleViewModelDelegate: class {
func someEventDidOccur()
}
class ExampleViewModel {
weak var delegate: ExampleViewModelDelegate?