Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
wh1pch81n / almostProtected.swift
Created November 5, 2016 01:43
Swift has no access modifier "Protected". Below is the closest thing to protected. You can call this semi-protected.
import UIKit
// discourage directly setting property m by using private(set). Anyone can still set the value of the property via setValue(_ :forKey:)
public class Abe: NSObject {
public private(set) var m: [String] = ["abc"]
}
public class Billy: Abe {
override init() {
@wh1pch81n
wh1pch81n / simplified_NS_STRING_ENUM.h
Created November 5, 2016 03:12
Using NS_STRING_ENUM can be verbose when used in objective c. Below is an example of how macros can be used to shorten it. These macros can be used and reused by cleverly defining and underlining macros.
//global macro helpers
#define DH_Str(I) #I
#define __DH_STRING_ENUM(PREFIX, SUFFIX, PREFIX2, SUFFIX2) \
static PREFIX PREFIX##_##SUFFIX = @DH_Str(PREFIX2##SUFFIX2);
#define _DH_STRING_ENUM2(PREFIX, SUFFIX, VAL) \
static PREFIX PREFIX##_##SUFFIX = VAL;
#define _DH_STRING_ENUM(T,I) \
__DH_STRING_ENUM(T, I, T, I)
//------ file dependent
// the enum type shall be DHBlahKey
@wh1pch81n
wh1pch81n / enum_count.swift
Last active November 5, 2016 06:12
A method to help count enums. The switch statement will enforce you cover a state where you count. This is in response to this: https://github.com/jtbandes/swift-evolution/blob/case-enumerable/proposals/0000-derived-collection-of-enum-cases.md
enum Planets {
case Mercury
case Venus
case Earth
case Mars
case Jupiter
static let count: Int = _count()
private static func _count(_ first: Planets = .Mercury) -> Int {
print("blahp")
@wh1pch81n
wh1pch81n / UIViewAnimation_as_block
Created December 7, 2016 19:02
using Currying on UIView Animation
public class func animation(withDuration duration: TimeInterval) -> (@escaping () -> ()) -> (@escaping (Bool) -> ()) -> Void {
return { (animations: @escaping () -> ()) in
return { (completion: @escaping (Bool) -> ()) in
UIView.animate(withDuration: duration, animations: animations, completion: completion)
}
}
}
@wh1pch81n
wh1pch81n / UIKit_Animator_API.swift
Created December 10, 2016 22:18
A wrapper that allows you to set animation and completion handler in a more consistent way
extension UIKit.UIView {
public class Animator {
fileprivate var animatorBlock: ((@escaping () -> (), @escaping (Bool) -> ()) -> ())
fileprivate var animations: () -> () = { _ in }
public init(animatorBlock: @escaping ((@escaping () -> (), @escaping (Bool) -> ()) -> ())) {
self.animatorBlock = animatorBlock
}
// 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]))
// 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 })
// 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
// 1
var buttonNotification = SwiftyNotification<String, String, Int>()
// 2
buttonNotification.add(subscriber: self) { (info, n) in
// 3
print(info)
// 4
n = 8
}
// 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>()