Skip to content

Instantly share code, notes, and snippets.

View brentsimmons's full-sized avatar

Brent Simmons brentsimmons

View GitHub Profile
@brentsimmons
brentsimmons / declarative_code.markdown
Last active April 30, 2019 14:16
A draft of a blog post that will never be published.

THIS IS A BLOG POST DRAFT THAT I WILL NEVER PUBLISH

INSTEAD I PLAN TO DO A SERIES OF SMALLER BLOG POSTS ON THE SUBJECT

The Objective-C Runtime and Declarative Programming

I write new code in Swift. It’s fun and I like it.

//: Playground - noun: a place where people can play
import Cocoa
// I’m trying to figure out how to add a Notification observer in Swift code where the notification name is defined in a .m file.
//
// This playground won’t actually run, because it assumes an NSString named SomeNotification defined in a .m file. So it can’t actually be used to see the errors. It’s just for illustration.
// The errors are listed in the comments.
class MyClass {
//: Playground - noun: a place where people can play
import Cocoa
var str = "Hello, playground"
protocol Message: class {
var uniqueID: String {get}
}
//: Playground - noun: a place where people can play
import Cocoa
// When you’re downloading objects from the web, it’s common to need to merge changes
// from the server to already-existing local objects. (If your data model allows for
// mutable objects, as with Core Data, that is.)
//
// The below is a Swift translation of how I’ve done this in Objective-C.
// Note that it works just fine in Swift — though it does require NSObject subclasses.
/* The rules outlined in Comparing Reactive and Traditional represent the business logic
for contacting the server: coalesce requests over a timeout period,
coalesce non-unique consecutive requests, and ignore requests shorter
than a specified length. If I’ve learnt anything in nearly 30 years
of writing software, it’s you don’t want to put business logic in the UI.
Working with UI is complicated enough without embedding your business logic there.
That’s why the business logic is embedded in the Fetcher object
– mostly in the -fetchQuery:error: method.
Because we’re coalescing calls, having a method with a completion handler
// This is the traditional version of the RxSwift-based gist posted here:
// https://gist.github.com/cliss/51cb740b14f3cd56ba1d11f2a9a6ba02
// This won’t compile and it surely has errors.
// (The same may be true as the original.)
// Some things are obviously omitted.
// It's meant as illustrative rather than as actual running code.
//
// The problem being solved:
// There is a text field. When you type in it, all changes are coalesced
// for 0.3 seconds, and then an HTTP call is made, and a table is updated
@brentsimmons
brentsimmons / SecretSanta.swift
Created September 25, 2015 17:30
My solution for the Omni Group’s Swift Bike Shedding Club week two question: http://rubyquiz.com/quiz2.html
//: Playground - noun: a place where people can play
import Cocoa
class Person: Hashable {
let firstName: String
let lastName: String
let email: String
//: Playground - noun: a place where people can play
import Cocoa
var aSet = Set<Int>()
aSet.insert(1)
aSet.insert(2)
aSet.insert(3)
// The rest of this code doesn't even remotely work.
//: Playground - noun: a place where people can play
import Cocoa
var str = "Hello, playground"
@objc protocol Thing {
func doThing(x: String) throws -> String
}
//: Playground - noun: a place where people can play
import Cocoa
protocol Account: Equatable {
var accountID: String {get}
}
// https://twitter.com/optshiftk/status/628985834801336320