Skip to content

Instantly share code, notes, and snippets.

import AVFoundation
class EACAudioManager: NSObject {
static let sharedInstance = EACAudioManager()
var volumeDidChange = { (newVolume: Float) -> () in }
var currentVolume: Float { return AVAudioSession.sharedInstance().outputVolume }
override init() {
try! AVAudioSession.sharedInstance().setActive(true)
AVAudioSession.sharedInstance().addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.New, context: nil)
wh1pch81n / TypeInferedVCFromStoryboard
Created March 31, 2016 14:34
In swift, if you instantiate the view controller from the storyboard identifier, you typically need to cast it. However, since the storyboard identifier is typically the same as the vc name, it might be perfectly acceptable to just pass in the type and convert that to a string. Since you passed in a type, the function can infer the return type.
extension UIStoryboard {
func instantiateViewControllerWithTypeAsIdentifier<T>(t: T.Type) -> T {
return instantiateViewControllerWithIdentifier(String(t)) as! T
wh1pch81n / ClickableLinksUITextView
Created April 14, 2016 03:45
UITextView supports clinking a link and going directly to the website, but sometimes you want that "link" to act like a button so you can do something else when you click it. In this example we have ""no-opclickno-op"" where "click" is a link that will print "hello world"
import UIKit
import XCPlayground
class ClickableLinkTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
required init?(coder aDecoder: NSCoder) {
wh1pch81n / objectSetUpInBlock.swift
Created April 17, 2016 19:20
Sometimes it is easier to read code where the set up is in a block.
import UIKit
You can use this to initalize an object (class or struct) while containing the set up code for it in a block
func setUpObject<T>(obj: T, withBlock setUp: (inout T) -> ()) -> T {
var _obj = obj
return _obj
UIView *getStatusBar() {
UIView *subViewWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
for (UIView *i in subViewWindow.subviews) {
if ([i isKindOfClass:NSClassFromString(@"UIStatusBar")]) {
return i;
return nil;
wh1pch81n / objcGenerics
Created May 22, 2016 03:17
Objective c generics for something that isn't a collection type. Here is an example of producing a view controller from storyboard where you are returned the correct type thanks to generics.
// Making a class since we can not add generics to UIStoryboard nor can we make a category for it.
@interface UIStoryboardHelper <__covariant T:UIViewController *> : UIStoryboard
- (T)vcFromIdentifier:(NSString *)identifier fromStoryboard:(UIStoryboard *)storyboard;
@implementation UIStoryboardHelper
- (UIViewController *)vcFromIdentifier:(NSString *)identifier fromStoryboard:(UIStoryboard *)storyboard {
return [storyboard instantiateViewControllerWithIdentifier:identifier];
wh1pch81n / implicitBundle.swift
Created June 10, 2016 03:25
In objective -c you can use [self class] to get the Class of an object. From there you can get the NSBundle. A smart man can use a macro when locating strings and make one such that [self class] is used to get the correct bundle. However, swift does not have macros. Below is a work around that does something similar.
import Foundation
original macro
#define DHLocalizedString(KEY, TABLE, COMMENT) NSLocalizedStringWithDefaultValue(KEY, TABLE, [NSBundle mainBundle], NSLocalizedStringWithDefaultValue(KEY, TABLE, [NSBundle bundleForClass:[self class]]), COMMENT)
func getNameByRemovingPrefixPathAndExtension(name: String) -> String {
let url = NSURL(string: name)!
wh1pch81n / Struct_Singleton_3ProtocolSplit.swift
Created July 9, 2016 18:05
And example of using a struct singleton with only static properties. The static properties are enforced with the protocol. We can still use dependency injection which means we can do away with the "sharedInstance" construct.
protocol Singleton3_Protocol {
static var queue: OperationQueue { get }
static func method1()
protocol Name {
static var name: String { get set }
protocol Singleton3_Protocol2: Name {
func supportsKeyboard(lang: String?) -> Bool {
guard let _lang = lang else {
return false
let predicates = [
Predicate(format: "SELF == %@", "en"),
Predicate(format: "SELF like[cd] %@", "en-*")
for predicate in predicates {
if NSArray(arrayLiteral: _lang).filtered(using: predicate).count > 0 {
wh1pch81n / UISearchBar_Magnifying_GlassColorChange_withSceneDock.swift
Created August 13, 2016 17:21
An alternative implementation for setting the magnifying glass color. You need to add this class to the scene dock of your View Controller in interface builder and connect the IBOutlet to the UISearchBar instance. The color for the component can be set on the object in storyboard. The although this is more work than the other approach you have t…
class UISearchBarMagnifyingGlass: NSObject {
@IBOutlet weak var searchBar: UISearchBar! {
didSet {
if let _color = color, let _searchBar = searchBar {
for i in _searchBar.subviews.first!.subviews {
if let textField = i as? UITextField,
let imageView = textField.leftView as? UIImageView,
let image = imageView.image
let coloredImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)