Skip to content

Instantly share code, notes, and snippets.

class ViewController: UIViewController {
var customizerObject: SpecialProtocol?
override func viewDidLoad() {
super.viewDidLoad()
specialMethod()
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
specialMethod()
}
func specialMethod() {
// overridable method
@wh1pch81n
wh1pch81n / programmaticallyTap.swift
Created February 9, 2017 02:22
A little extension that lets you "tap" a UIBarButtonItem programmatically
extension UIBarButtonItem {
public func programmaticallyTap() {
if let target = target
, let action = action
{
target.value(forKey: NSStringFromSelector(action))
}
}
}
@wh1pch81n
wh1pch81n / enumstring_as_class.swift
Created January 7, 2017 19:33
NS_ENUM_STRING ports a struct with static strings. This means between objc and swift you have to learn how to read both versions of it which can be confusing. It is much better to have a more consistent api between both. Therefore you can use a class.
func ==(lhs: EnumString, rhs: String) -> Bool {
return lhs.isEqual(rhs)
}
func ==(lhs: String, rhs: EnumString) -> Bool {
return rhs.isEqual(lhs)
}
class EnumString: NSObject {
let rawValue: String
@wh1pch81n
wh1pch81n / concatenating_errors.swift
Last active January 7, 2017 17:28
What is one trick you can do to avoid the do-catch pyramid of doom? you can return "try method()"
enum ErrorEnum: Error {
case FooError
case BarError
}
func bar(success: Bool) throws -> String {
if success {
return "bar success"
}
throw ErrorEnum.BarError
// 1
public struct SwiftyNotificationCenter {}
// ...
// extended somewhere else in your code base
// 2
extension SwiftyNotificationCenter {
static var buttonNotification = SwiftyNotification<String, String, Int>()
static var anotherNotification = SwiftyNotification<String, UIColor, Bool>()
// 1
var buttonNotification = SwiftyNotification<String, String, Int>()
// 2
buttonNotification.add(subscriber: self) { (info, n) in
// 3
print(info)
// 4
n = 8
}
// DHConstraintBuilder
//1
view_cb.addConstraints(() |-^ greenView_cb ^-^ 15.5 ^-^ redView_cb ^-| ()).H
//2
view_cb.addConstraints(() |-^ blueView_cb ^-| ()).H
//3
view_cb.addConstraints(() |-^ greenView_cb ^-^ blueView_cb ^-| ()).V
// NSLayoutConstraint
// Given an array of 3 views
let viewArray = [
greenView_vf,
redView_vf,
blueView_vf
]
// set this value to false to make auto layout work
viewArray.forEach({ $0.translatesAutoresizingMaskIntoConstraints = false })
// Adding a vertical constraint between view1 and view2 using default padding
// DHConstraintBuilder
parentView.addConstraints(view1 ^-^ view2).V
// NSLayoutConstraints
parentView.translatesAutoresizingMaskIntoConstraints = false
parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[view1]-[view2]"
, options: NSLayoutFormatOptions(rawValue: 0)
, metrics: nil, views: ["view1": view1, "view2": view2]))