This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
var buttonNotification = SwiftyNotification<String, String, Int>() | |
// 2 | |
buttonNotification.add(subscriber: self) { (info, n) in | |
// 3 | |
print(info) | |
// 4 | |
n = 8 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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>() |