Skip to content

Instantly share code, notes, and snippets.

import Foundation
struct MinHeap {
var items: [Int] = []
//Get Index
private func getLeftChildIndex(_ parentIndex: Int) -> Int {
return 2 * parentIndex + 1
}
private func getRightChildIndex(_ parentIndex: Int) -> Int {
// Old way of doing things.
let actionButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(someMethod()))
// New way of doing things.
let button = UIBarButtonItem(title: "Done", style: .done) { _ in
// Do stuff here.
}
convenience init(title: String?, style: UIBarButtonItem.Style, closure: @escaping UIBarButtonItemTargetClosure) {
self.init(title: title, style: style, target: nil, action: nil)
targetClosure = closure
action = #selector(UIBarButtonItem.closureAction)
}
@objc func closureAction() {
guard let targetClosure = targetClosure else { return }
targetClosure(self)
}
@BeauNouvelle
BeauNouvelle / UIBarButtonItem Closure 1.swift
Last active April 11, 2019 02:46
UIBarButtonItem Closure
extension UIBarButtonItem {
/// Typealias for UIBarButtonItem closure.
private typealias UIBarButtonItemTargetClosure = (UIBarButtonItem) -> ()
private class UIBarButtonItemClosureWrapper: NSObject {
let closure: UIBarButtonItemTargetClosure
init(_ closure: @escaping UIBarButtonItemTargetClosure) {
self.closure = closure
}
@gregsabo
gregsabo / AudioKitCheatSheet.markdown
Last active June 27, 2021 22:03
AudioKit Cheat Sheet

Play a sine wave.

import AudioKit

let osc = AKOscillator()
osc.frequency = 440.0
osc.start()
AudioKit.output = osc
AudioKit.start()
@dclelland
dclelland / UIImage+Extensions.swift
Created September 17, 2017 11:48
Testing out the new UIGraphicsImageRenderer class on iOS 11
import UIKit
extension UIImage {
convenience init?(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) {
let image = UIGraphicsImageRenderer(size: size).image { context in
color.setFill()
context.fill(CGRect(origin: .zero, size: size))
}
@loucimj
loucimj / Diffable.swift
Last active April 12, 2018 16:38
Diffable workaround for structs with IGListKit
//
// Diffable.swift
//
// Created by danielgalasko on 8/5/17.
//
import Foundation
import IGListKit
import Foundation
extension String {
func snakecased() -> String {
return self.replacingOccurrences(of: "(?<=[^A-Z])([A-Z])", with: "_$0", options: .regularExpression).lowercased()
}
}
@cmoulton
cmoulton / Custom HTTP Headers with Swift and Alamofire.swift
Last active July 15, 2022 12:37
Custom HTTP Headers with Swift 3 or 4 and Alamofire 4.0-4.7: See https://grokswift.com/custom-headers-alamofire4-swift3/ for explanations
// MARK: - Adding a header to a single request
func doRequestWithHeaders1() {
let headers: HTTPHeaders = [
"X-Mashape-Key": MY_API_KEY,
"Accept": "application/json"
]
Alamofire.request("https://mashape-community-urban-dictionary.p.mashape.com/define?term=smh", headers: headers)
.responseJSON { response in
debugPrint(response)
@imaizume
imaizume / .gitignore
Last active September 22, 2021 11:59
TrelloのカードをSpreadSheetで記録するGoogle Apps Script
# Created by https://www.gitignore.io/api/webstorm
### WebStorm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea