Skip to content

Instantly share code, notes, and snippets.

View worthbak's full-sized avatar

Worth Baker worthbak

View GitHub Profile
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// 1
if let item = annotation as? MapItem {
// 2
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "mapItem")
?? MKAnnotationView(annotation: annotation, reuseIdentifier: "mapItem")
annotationView.annotation = item
annotationView.image = UIImage(named: "annotation")
// 3
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
for randCoordinate in makeRandomCoordinates(in: mapView.region) {
let annotation = MapItem(coordinate: randCoordinate)
mapView.addAnnotation(annotation)
}
}
/// Using `blockURLComponent`, requests relenvant data from FRING API for the given blocks. Before
/// requesting, it will check the `fetchedBlockIds` set, attempting to avoid requesting duplicate
/// information. After data is fetched, `processOperation` will be called.
func requestData(for blockIds: [(MKShape, String)]) -> Observable<[[String: Any]]> {
assert(parse != nil, "parse closure was never set; this is a requirement of use")
let rv = PublishSubject<[[String: Any]]>()
// bail if we already have all block data
let filteredBlockIds = blockIds
@worthbak
worthbak / xcopen.sh
Last active May 7, 2017 13:36
A simple bash script that will attempt to open a Xcode workspace or project in the current directory.
#!/bin/bash
for i in *; do
if [[ $i == *.xcworkspace ]]; then
echo "found $i; opening"
open $i
exit 0
fi
done
@worthbak
worthbak / CoordinateDistanceCalculatable.swift
Last active January 14, 2016 23:21
A riff on Erica Saudi's Swift-y implementation of a distance calculator between two coordinates (http://ericasadun.com/2016/01/11/make-this-swift-er-coordinate-distances/)
import CoreLocation
import Darwin
private enum EarthUnits: Double {
case ImperialRadius = 3961.0
case MetricRadius = 6373.0
}
postfix operator ° {}
postfix func °(degrees: Double) -> Double {return degrees * M_PI / 180.0}
@worthbak
worthbak / ClimbingGearSequenceType.swift
Created November 12, 2015 15:15
A quick, basic implementation of SequenceType that represents branded climbing gear (why not!), created just to toss some concrete ideas around.
struct ClimbingGear: SequenceType {
let brand: String
let gear: [String]
typealias Generator = AnyGenerator<String>
func generate() -> Generator {
var i = 0
return anyGenerator {
if i < self.gear.count {
@worthbak
worthbak / UIView_replaceWithNewView(:withAnimation:completion:).swift
Created July 30, 2015 21:10
UIView extension for replacing one view with another
extension UIView {
func replaceWithNewView(newView: UIView, withAnimation animated: Bool, completion: ((Bool) -> Void)?) {
if let oldSuperview = self.superview {
newView.frame = self.frame
newView.transform = CGAffineTransformMakeScale(0.1, 0.1)
oldSuperview.addSubview(newView)