Skip to content

Instantly share code, notes, and snippets.

@ateliercw
ateliercw / swiftilities_deselect.swift
Last active June 10, 2016 03:44
An un-curried deselect function, for comparison with curried_deselect
extension UIViewController {
func rz_smoothlyDeselectRows(tableView tableView: UITableView?) {
let selectedIndexPaths = tableView?.indexPathsForSelectedRows ?? []
if let coordinator = transitionCoordinator() {
coordinator.animateAlongsideTransitionInView(parentViewController?.view, animation: { context in
selectedIndexPaths.forEach {
tableView?.deselectRowAtIndexPath($0, animated: context.isAnimated())
}
@ateliercw
ateliercw / curried_deselect.swift
Last active June 10, 2016 20:11
Comparing a curried and un-curried deselect rows function
extension UIViewController {
func rz_smoothlyDeselectRows(tableView tableViewIn: UITableView?) {
guard let tableView = tableViewIn,
selectedIndexPaths = tableView.indexPathsForSelectedRows else {
return
}
let deselect = { (tableView: UITableView, animated: Bool) -> ((NSIndexPath) -> ()) in
@ateliercw
ateliercw / ForceAppearance.swift
Last active May 13, 2016 02:17
Showing UIAppearance Proxies on on values that aren't normally exposed
private extension AppStyle {
func setupAppearance() {
setupNavBarAppearance()
setupTableViewCellAppearance()
setupTableAppearance()
}
func setupNavBarAppearance() {
public extension UIViewKeyframeAnimationOptions {
// This exists because keyframe options doesn't expose the easing for the overall curve
public init(keyframeAnimationsOptions: UIViewKeyframeAnimationOptions, animationCurve: UIViewAnimationCurve) {
let animationOptions: UIViewAnimationOptions
switch animationCurve {
case .EaseIn: animationOptions = .CurveEaseIn
case .EaseInOut: animationOptions = .CurveEaseInOut
case .EaseOut: animationOptions = .CurveEaseOut
case .Linear: animationOptions = .CurveLinear
}
@ateliercw
ateliercw / ErrorDecoding.swift
Last active April 8, 2016 03:23
Showing off using errors to speed up JSON decoding
import Foundation
enum JSONDecodingError: ErrorType {
case NilValueForProperty
case TypeMismatch
}
extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject {
// Adds a Swift-y way to get a non-optional type from a key
func differenceFrom(otherRGBA: UIImage.RGBA) -> Int {
let aDiff = Int(max(alpha, otherRGBA.alpha) - min(alpha, otherRGBA.alpha))
let rDiff = Int(max(red, otherRGBA.red) - min(red, otherRGBA.red))
let gDiff = Int(max(green, otherRGBA.green) - min(green, otherRGBA.green))
let bDiff = Int(max(blue, otherRGBA.green) - min(blue, otherRGBA.blue))
return aDiff + rDiff + gDiff + bDiff
}
func differenceFrom(otherRGBA: UIImage.RGBA) -> Int {
return Int(max(alpha, otherRGBA.alpha) - min(alpha, otherRGBA.alpha)) +
Int(max(red, otherRGBA.red) - min(red, otherRGBA.red)) +
Int(max(green, otherRGBA.green) - min(green, otherRGBA.green)) +
Int(max(blue, otherRGBA.green) - min(blue, otherRGBA.blue))
}
@ateliercw
ateliercw / xcode_timing_to_json.rb
Created February 16, 2016 02:46
Parses xcode timing data to json
#!/usr/bin/env ruby
require 'json'
def parse_line(line)
units = line.split("\t")
hash = {}
hash[:time] = parse_time(units[0])
hash[:file] = parse_file(units[1])
hash[:function] = units[2]
@ateliercw
ateliercw / profile.sh
Last active May 17, 2016 18:14
Basic worst offenders invocation
xcodebuild -workspace [workspace] -scheme [scheme] clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep .[0-9]ms | grep -v ^0.[0-9]ms | sort -nr > culprits.txt
@ateliercw
ateliercw / AutomaticDequeue.swift
Last active February 18, 2016 22:04
Rough example of improving table view cell registration and dequeuing via generics
protocol AutomaticDequeue {
}
extension AutomaticDequeue where Self: AnyObject {
static var reuseIdentifer: String {
return NSStringFromClass(self) as String
}
}