Skip to content

Instantly share code, notes, and snippets.

@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
}
@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 / 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 / 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 / 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 / page_names.swift
Last active October 8, 2016 03:12
An example of stringifying an enum string state
enum PageNames: String {
case sc🖍search = "MySearch"
var page_name: String {
return String(describing: self).replacingOccurrences(of: "🖍", with: "/")
}
}
PageNames.sc🖍search.rawValue // "MySearch"
// This is a conditional inclusion.
// It searches for a file called Captain.xcconfig that lives one file level up in relation to this file.
#include? "../Captain.xcconfig"
#if ABC
// Code between here that runs only when ABC exists
#endif
ABC_OBJC=ABC=1
ABC_SWIFT=ABC
#if DEBUG
print("print debug value", x)
#endif