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() {
super.init()
try! AVAudioSession.sharedInstance().setActive(true)
AVAudioSession.sharedInstance().addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.New, context: nil)
@wh1pch81n
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
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)
setUp()
}
required init?(coder aDecoder: NSCoder) {
@wh1pch81n
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
*/
@warn_unused_result
func setUpObject<T>(obj: T, withBlock setUp: (inout T) -> ()) -> T {
var _obj = obj
setUp(&_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
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;
@end
@implementation UIStoryboardHelper
- (UIViewController *)vcFromIdentifier:(NSString *)identifier fromStoryboard:(UIStoryboard *)storyboard {
return [storyboard instantiateViewControllerWithIdentifier:identifier];
}
@end
@wh1pch81n
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
wh1pch81n / SingletonSubclassing.swift
Last active February 19, 2022 02:01
The Swift way of doing Singleton inheritance.
import UIKit
class Singleton {
class func sharedInstance() -> Singleton {
struct __ { static let _sharedInstance = Singleton() }
return __._sharedInstance
}
var name: String = String()
}
@wh1pch81n
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 {
@wh1pch81n
wh1pch81n / GenericTableView.swift
Last active December 16, 2017 07:51
Sometimes it is easier to make a tableview generic. And just load it with info, rather than creating a whole class that conforms with UITableViewDataSource and UITableViewDelegate
import UIKit
class MyTableViewCell: UITableViewCell {
@IBOutlet var switchview: UISwitch!
@IBOutlet var sliderview: UISlider!
}
class Section {
var count: Int { return cells.count }