Skip to content

Instantly share code, notes, and snippets.

View chrishulbert's full-sized avatar

Chris Hulbert chrishulbert

View GitHub Profile
@chrishulbert
chrishulbert / main.rs
Created August 27, 2023 13:15
Interactive Brokers TWS API in Rust
// MIT licensed.
use std::net::TcpStream;
use std::io::{Read, Write};
use std::sync::mpsc::channel;
use std::thread;
fn main() {
let mut stream = TcpStream::connect("127.0.0.1:7496").expect("connect");
stream.set_nodelay(true).expect("nodelay"); // Because we're wannabe HFT traders.
find . -type f \( -name '*ViewController.m' -or -name '*ViewController.swift' \) -exec grep -L -e 'preferredStatusBarStyle' {} +
@chrishulbert
chrishulbert / Property.swift
Created July 23, 2015 10:14
Example of a simple Swift alternative to KVO. Inspired by reactive cocoa's MutableProperty. Weakly-referenced subscribers and all.
import Foundation
// Simple observable property
// Careful: Not thread safe! You should use this only from the main thread, or modify to
// have locks.
class Property<T> {
private var _value: T
var value: T {
get { return _value }
set {
import Foundation
import Security
struct SecItemWrapper {
static func matching(query: NSDictionary) -> (status: Status, result: AnyObject?) {
var rawResult: Unmanaged<AnyObject>?
let rawStatus = SecItemCopyMatching(query, &rawResult)
let result: AnyObject? = rawResult?.takeRetainedValue()
let status = Status.fromOSStatus(rawStatus)
return (status, result)
//
// Keychain.swift
// SwiftKeychain
//
// Created by Chris Hulbert on 14/06/2015.
// Copyright (c) 2015 Chris Hulbert. All rights reserved.
//
import Foundation
import Security
@chrishulbert
chrishulbert / MyNavigationController.m
Created March 5, 2015 03:10
Allows you to slide your navigation bar away when hiding it
// Header:
@interface MyNavigationController : UINavigationController
- (void)setNavigationBarSlidAway:(BOOL)slidAway animated:(BOOL)animated;
@end
// Implementation:
@chrishulbert
chrishulbert / WeakDelegates.swift
Created March 3, 2015 04:26
Swift implementation of C#-style Events concept (eg a manager with multiple weakly-referenced delegates)
import Foundation
/// Since there can be multiple delegates, they can only be used to signifying events (output), not for
/// supplying data for the manager (input).
@objc protocol MyManagerDelegate {
func manager(manager: MyManager, isNowLoading: Bool)
}
@objc class MyManager {
import UIKit
extension UIColor {
/// Creates a UIColor from an 'rrggbb' hex string.
class func fromHex(hexColour: String) -> UIColor {
let str = hexColour.cStringUsingEncoding(NSUTF8StringEncoding)
let x = strtol(str!, nil, 16)
let r = x >> 16
let g = (x >> 8) & 0xff
!!! There's a better solution here now, that doesn't require swizzling:
!!! http://www.splinter.com.au/2014/09/10/afnetworking-error-bodies/
!!! But i'll leave the below swizzling solution up for old time's sake
// AFURLSessionManager+ErrorResponse.h
// This hacks AFURLSessionManager so that it returns the error response in failure callbacks, in the error's userInfo.
// Usage:
// [mySessionManager POST:@"some_api_endpoint" parameters:params success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
@chrishulbert
chrishulbert / MyButton.m
Last active December 5, 2015 09:41
UIButton subclass
// How to subclass UIButton, given that it uses a static constructor so you can't override init.
@interface MyButton : UIButton
...
@end
@implementation MyButton
+ (instancetype)buttonWithType:(UIButtonType)buttonType {
MyButton *button = [super buttonWithType:buttonType];