Skip to content

Instantly share code, notes, and snippets.

/// via: https://oleb.net/blog/2018/01/notificationcenter-removeobserver/
/// Wraps the observer token received from
/// NotificationCenter.addObserver(forName:object:queue:using:)
/// and unregisters it in deinit.
final class NotificationToken: NSObject {
let notificationCenter: NotificationCenter
let token: Any
init(notificationCenter: NotificationCenter = .default, token: Any) {
self.notificationCenter = notificationCenter
extension SKProduct {
func localizedPrice(divisor: Int) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = self.priceLocale
return formatter.string(from:
self.price.dividing(by: NSDecimalNumber(value: divisor),
withBehavior: NSDecimalNumberHandler(roundingMode: .down,
scale: 2,
@burakyanmaz
burakyanmaz / MD5.swift
Created September 20, 2018 06:30 — forked from pietbrauer/MD5.swift
NSString & NSData to MD5
import Foundation
extension NSData {
func MD5() -> NSString {
let digestLength = Int(CC_MD5_DIGEST_LENGTH)
let md5Buffer = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLength)
CC_MD5(bytes, CC_LONG(length), md5Buffer)
var output = NSMutableString(capacity: Int(CC_MD5_DIGEST_LENGTH * 2))
for i in 0..<digestLength {
@burakyanmaz
burakyanmaz / NavigationControllerDelegateForChildVCDeallocation.swift
Last active September 19, 2018 13:06
Consider a root table view controller containing hundreds of rows. The user pushes and then pops every row. What this does is basically, immediately after a view controller is popped out from the stack of a navigation controller, freeing up the memory from still storing the view controller that has just popped.
import UIKit
protocol ChildDeallocationProtocol {
func deallocateChildVCs()
}
class NavigationControllerDelegateForChildVCDeallocation: NSObject {
}
extension NavigationControllerDelegateForChildVCDeallocation: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
@burakyanmaz
burakyanmaz / database.rules.json
Created September 1, 2018 13:11 — forked from codediodeio/database.rules.json
Common Database Rules for Firebase
// No Security
{
"rules": {
".read": true,
".write": true
}
}
@burakyanmaz
burakyanmaz / LazyInitialization.swift
Last active February 9, 2018 08:42
Lazy initialization of a variable
// This is just a great implementation :D
lazy var resumeTaskQueue: TaskQueue = {
let _resumeTaskQueue = TaskQueue()
_resumeTaskQueue.maxConcurrentTaskCount = 10
return _resumeTaskQueue
}()
@burakyanmaz
burakyanmaz / SaveManager.swift
Created March 26, 2017 10:17 — forked from cemolcay/SaveManager.swift
NSUserDefaults Save Manager
// MARK: - Manager
class SaveManager {
// MARK: Singleton
private static let instance = SaveManager()
class func sharedInstance () -> SaveManager {
@burakyanmaz
burakyanmaz / GenericFunctionAppendingDictionary.swift
Created March 20, 2017 16:24
Dictionary addition to another dictionary
func += <K, V> ( left: inout [K:V], right: [K:V]) {
for (k, v) in right {
left.updateValue(v, forKey: k)
}
}