Skip to content

Instantly share code, notes, and snippets.

@tomconroy
tomconroy / Event.swift
Last active December 22, 2016 20:57
A simple Event Emitter for swift (use EmitterKit for something more robust)
class Event <T:Any> {
var handlers = Array<(T) -> Void>()
func listen(handler: (T) -> Void) {
handlers.append(handler)
}
func emit(object: T) {
for handler in handlers {
handler(object)
@Kaelten
Kaelten / synced.swift
Last active August 29, 2015 14:02
Simple Swift @synchronized Helper
// A simple replacement for Obj-C's @synchronized keyword, currently seems to cause compiler error if lock is an object array.
func synced(lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
class DataBuffer {
var internalData: NSData;
init(fromData: NSData) {
self.internalData = NSData.dataWithData(fromData) as NSData;
}
init(fromFilePath: String) {
self.internalData = NSData.dataWithContentsOfFile(fromFilePath, options: .DataReadingMappedIfSafe, error: nil) as NSData;
}
@mchambers
mchambers / reflect.swift
Last active March 5, 2021 09:20
Basic Reflection in Swift.
// Let's define a basic Swift class.
class Fruit {
var type=1
var name="Apple"
var delicious=true
}
// We can get at some info about an instance of an object using reflect(), which returns a Mirror.
reflect(Fruit()).count
reflect(Fruit())[1].0
@justinko
justinko / Plea.markdown
Created May 30, 2012 19:40
Am I doing it wrong?

Dear Rubyists,

I just lost a contract because of my code in a Rails project.

The specific code in question is related to a "posting a comment" feature. Here are the details:

In this project, "posting a comment" does not simply entail inserting a row into the database. It involves a procedure to yes, insert a row, but also detect its language, check for spam, send emails, and "share" it to Twitter and Facebook. I believe this algorithm should be encapsulated. I do not believe it belongs in a controller or a model. I do not believe Active Record callbacks should be used.

The "senior developer", whom is the stake holder's right hand man, said this: