Skip to content

Instantly share code, notes, and snippets.

import Foundation
import SwiftyJSON
extension JSON {
func update(keyPath: String, value: JSON?) -> JSON {
let keys = keyPath.componentsSeparatedByString(".")
guard let targetKey = keys.first else {
return self
}
@Ridwy
Ridwy / URLRouter.swift
Last active February 12, 2016 13:05
Swift URL Router
class URLRouter {
let placeholders: [String: String]
init(placeholders: [String: String] = [:]) {
self.placeholders = placeholders
}
typealias ActionHandler = ((NSURL)->())
func register(path: String, handler: ActionHandler) -> Self {
let pathWithoutRoot = path.hasPrefix("/") ? path.substringFromIndex(path.startIndex.advancedBy(1)) : path
var node = root
extension String {
var md5: String? {
guard let str = cString(using: .utf8) else { return nil }
let strLen = CC_LONG(lengthOfBytes(using: .utf8))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
CC_MD5(str, strLen, result)
let hash = (0 ..< digestLen).map({String(format:"%02x", result[$0])}).joined()
@Ridwy
Ridwy / UIViewControllerScreenshot.swift
Created August 8, 2017 04:15
take screenshots of each UIViewController
import UIKit
import AVFoundation
// call this in application(_:didFinishLaunchingWithOptions:)
func setupDebugScreenshot() {
let from = class_getInstanceMethod(UIViewController.self, #selector(UIViewController.viewDidAppear))
let to = class_getInstanceMethod(UIViewController.self, #selector(UIViewController.viewDidAppearWithScreenshot))
method_exchangeImplementations(from, to)
}
@Ridwy
Ridwy / MTAudioProcessingTapSample.swift
Created July 30, 2021 11:21
How to use MTAudioProcessingTap in Swift 5
//
// Created by Chiharu Nameki @Ridwy on 2021/07/30.
//
import UIKit
import AVFoundation
/*
Using MTAudioProcessingTap, you can touch raw audio samples playing with AVPlayer.
This sample code shows how to use MTAudioProcessingTap in Swift 5.