Skip to content

Instantly share code, notes, and snippets.

View cemolcay's full-sized avatar
🎼
🎶

Cem Olcay cemolcay

🎼
🎶
View GitHub Profile
@cemolcay
cemolcay / gist:7d54d62f0e94ef09f44ec57622a3b12d
Created May 7, 2019 12:23
Delete .DS_Store recursively in a folder
find . -name '.DS_Store' -type f -delete
@cemolcay
cemolcay / CodableStore.swift
Last active January 24, 2019 11:25
A protocol where can read/write Codable items on disk.
protocol DictionaryStore {
static var storeFileName: String { get }
var store: [[String: Any]] { get set }
}
extension DictionaryStore {
func write() throws {
let data = try JSONSerialization.data(withJSONObject: store, options: [])
let document = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let path = document.appendingPathComponent(Self.storeFileName)
@cemolcay
cemolcay / DictionaryMergeExtension.swift
Created January 23, 2019 12:36
Adds + and += operators to dictionary
/// Merges right hand side dictionary into left hand side dictionary. Works on nested dictionaries as well.
///
/// - Parameters:
/// - lhs: Dictionary you want to merge someting.
/// - rhs: Merging dictionary.
/// - Returns: Returns merged dictionary.
internal func +<Key, Value> (lhs: [Key: Value], rhs: [Key: Value]) -> [Key: Value] {
var result = lhs
rhs.forEach {
if let dict = $1 as? [Key: Value] {
@cemolcay
cemolcay / UIViewController+TopMostChild.swift
Created October 29, 2018 07:19
Returns the top most child in a view controller.
import UIKit
extension UIViewController {
var topMostChild: UIViewController? {
if let tab = self as? UITabBarController {
return tab.selectedViewController?.topMostChild
} else if let nav = self as? UINavigationController {
return nav.topViewController?.topMostChild
} else if let split = self as? UISplitViewController {
return split.viewControllers.last?.topMostChild
@cemolcay
cemolcay / CircularArrayExtension.swift
Created October 8, 2018 01:52
An array subscript extension that returns the element from the positive or negative circular index.
import Foundation
extension Array {
/// An array subscript extension that returns the element from the positive or negative circular index.
public subscript(circular index: Int) -> Element? {
guard count > 0 else { return nil }
let mod = index % count
let offset = index >= 0 ? 0 : count
let idx = mod == 0 ? 0 : mod + offset
return self[idx]
@cemolcay
cemolcay / machAbsoluteToSeconds.swift
Last active March 4, 2020 08:10
mach_absolute_time to seconds in Swift
var timebaseInfo = mach_timebase_info_data_t()
init() {
mach_timebase_info(&timebaseInfo)
}
func machAbsoluteToSeconds(machAbsolute: UInt64 = mach_absolute_time()) -> Double {
let nanos = Double(machAbsolute * UInt64(timebaseInfo.numer)) / Double(timebaseInfo.denom)
return nanos / 1.0e9;
}
@cemolcay
cemolcay / ConvertableRange.swift
Created February 20, 2018 12:02
Converts a value in a range to another range in Swift 4.0
import Foundation
func convert<T: FloatingPoint>(value: T, inRange: ClosedRange<T>, toRange: ClosedRange<T>) -> T {
let oldRange = inRange.upperBound - inRange.lowerBound
let newRange = toRange.upperBound - toRange.lowerBound
return (((value - inRange.lowerBound) * newRange) / oldRange) + toRange.lowerBound
}
func convert<T: SignedInteger>(value: T, inRange: ClosedRange<T>, toRange: ClosedRange<T>) -> T {
let oldRange = inRange.upperBound - inRange.lowerBound
@cemolcay
cemolcay / ABLLinkManager.swift
Last active December 2, 2018 21:42
Swift 4.0 port for LinkKit iOS of Ableton Link
//
// ABLLinkManager.swift
//
// Created by Cem Olcay on 5.03.2018.
// Copyright © 2018 cemolcay. All rights reserved.
//
import Foundation
import AVFoundation
func convertValue(oldValue: CGFloat, oldMin: CGFloat, oldMax: CGFloat, newMin: CGFloat, newMax: CGFloat) -> CGFloat {
let oldRange = oldMax - oldMin
let newRange = newMax - newMin
return (((oldValue - oldMin) * newRange) / oldRange) + newMin
}
@cemolcay
cemolcay / circular-array-subscript.swift
Created October 5, 2017 07:03
Swift circular array subscript
extension Array {
subscript(circular index: Int) -> Element? {
var i = index
if i < 0 || isEmpty {
return nil
} else if i > count - 1 {
i = index % count
}
return self[i]
}