Skip to content

Instantly share code, notes, and snippets.

Avatar

Tyler Stromberg AquaGeek

View GitHub Profile
@AquaGeek
AquaGeek / GitHubAPI.swift
Created Aug 19, 2020
Example of how to use SignalProducer to fetch multiple pages
View GitHubAPI.swift
private func fetchAllUsers() -> SignalProducer<[User], Error> {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
func fetchUsers(since lastSeenUserID: Int?) -> SignalProducer<[User], Error> {
var components = URLComponents(string: "https://api.github.com/users")!
if let userID = lastSeenUserID {
components.queryItems = [
URLQueryItem(name: "since", value: userID.description)
@AquaGeek
AquaGeek / XcodeKit.h
Created Apr 9, 2020
XcodeKit Headers
View XcodeKit.h
//
// Generated by class-dump 3.5 (64 bit).
//
// class-dump is Copyright (C) 1997-1998, 2000-2001, 2004-2013 by Steve Nygard.
//
#pragma mark Blocks
typedef void (^CDUnknownBlockType)(void); // return type and parameters are unknown
View L10n.swift
struct L10n {
struct Login {
static func welcomeMessage(placeholder: String) -> String {
return String.localizedStringWithFormat(
NSLocalizedString("login.welcome_message", "Greeting to show users upon successful login"),
placeholder
)
}
}
}
View ColorExtensions.swift
extension NSColor {
// Derived from logic in TextMate
func soften(color inColor: NSColor, factor: CGFloat) -> NSColor {
guard let color = inColor.usingColorSpace(NSColorSpace.sRGB) else {
return inColor
}
var red = color.redComponent
var green = color.greenComponent
var blue = color.blueComponent
@AquaGeek
AquaGeek / Tips.md
Created Mar 22, 2017
macOS/AppKit tips
View Tips.md

Override stupid system default that disables state restoration.

UserDefaults.standard.set(true, forKey: "NSQuitAlwaysKeepsWindows")  // override stupid global preference, so our state restore works properly
@AquaGeek
AquaGeek / Manifesto.md
Created Feb 17, 2017
Swift Ownership Manifesto
View Manifesto.md

Ownership

Introduction

Adding "ownership" to Swift is a major feature with many benefits for programmers. This document is both a "manifesto" and a "meta-proposal" for ownership: it lays out the basic goals of the work, describes a general approach for achieving those goals, and proposes a number of specific changes and features, each of which will need to be separately discussed in a smaller and more

View Rx.swift
// Retry with backoff
// Adapted from http://blog.danlew.net/2016/01/25/rxjavas-repeatwhen-and-retrywhen-explained/
Observable
.range(start: 1, count: 10)
.doOn(onNext: { i in
if i == 5 {
throw NSError(domain: "com.example", code: 1, userInfo: ["any": "thing"])
}
})
.retryWhen { (attempts: Observable<ErrorType>) -> Observable<Int64> in
View Storyboards.swift
// FROM: http://ankit.im/swift/2016/01/18/first-pure-swift-ios-app-swiflytics/
protocol StoryboardInstantiable {
typealias ViewController
static var storyboardID: String { get }
static func instance(storyboard: UIStoryboard) -> ViewController?
}
extension StoryboardInstantiable {
static func instance(storyboard: UIStoryboard) -> ViewController? {
return storyboard.instantiateViewControllerWithIdentifier(Self.storyboardID) as? ViewController
View AdjacentList.m
@interface TSGraph : NSObject
- (void)addEdgeFrom:(id)vertexA to:(id)vertexB;
@end
@implementation TSGraph
{
NSMutableDictionary *_vertices;
}
@AquaGeek
AquaGeek / main.m
Created May 23, 2015
Objective-C command line tool
View main.m
@import Foundation;
int main(int argc, const char * argv[])
{
__block BOOL keepRunning = YES;
// Configure a dispatch source to listen for SIGTERM
// Adapted from https://mikeash.com/pyblog/friday-qa-2011-04-01-signal-handling.html
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
You can’t perform that action at this time.