Skip to content

Instantly share code, notes, and snippets.

@dduan
dduan / UIColor+IntegerValue.h
Last active August 29, 2015 13:55
A category that converts UIColor to int32_t for convenient persistence.
//
// UIColor+SingleValue.h
//
// Created by Daniel Duan on 1/26/14.
// BSD License
//
#import <UIKit/UIKit.h>
@interface UIColor (SingleValue)
- (UIColor *)initWithInteger: (int32_t)integer;

Keybase proof

I hereby claim:

  • I am dduan on github.
  • I am duan (https://keybase.io/duan) on keybase.
  • I have a public key whose fingerprint is 2D75 873D D420 18FB 5B6B 72C5 4876 9046 F6CC 539C

To claim this, I am signing this object:

Verifying myself: My Bitcoin username is +duan. https://onename.io/duan
@dduan
dduan / DeselectableSegmentedControl.swift
Created December 10, 2014 06:57
A Deselectable UISegmentedControl
import UIKit
class DeselectableSegmentedControl: UISegmentedControl {
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
let previouslySelectedIndex = self.selectedSegmentIndex
super.touchesEnded(touches, withEvent: event)
if previouslySelectedIndex == self.selectedSegmentIndex {
self.selectedSegmentIndex = UISegmentedControlNoSegment
self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
}
@dduan
dduan / BorderedButton.swift
Last active August 29, 2015 14:11
UIButton with border.
//
// BorderedButton.swift
//
// Created by Daniel Duan on 12/13/14.
import UIKit
class BorderedButton: UIButton {
override func willMoveToSuperview(newSuperview: UIView?) {
@dduan
dduan / Complex.swift
Last active August 29, 2015 14:12 — forked from mattt/Complex.swift
struct Complex<T: FloatLiteralConvertible> {
var real: T
var imaginary: T
}
func +(lhs: Complex<Double>, rhs: Complex<Double>) -> Complex<Double> {
return Complex<Double>(real: lhs.real + rhs.real, imaginary: lhs.imaginary + rhs.imaginary)
}
func -(lhs: Complex<Double>, rhs: Complex<Double>) -> Complex<Double> {
@dduan
dduan / ChaingPuzzleWithSwift2b2.swift
Last active August 29, 2015 14:23
Is there a better way to linearize the following construct?
// Swift 2 Beta 2 to the rescue!
protocol A {
var b: B { get set }
}
protocol B {
func handle()
}
extension B {
@dduan
dduan / Makefile
Last active August 29, 2015 14:24
Unix 'cat' Command Implemented In Swift Without Foundation
SDKPATH = $(shell xcrun --show-sdk-path --sdk macosx)
CBRIDGEHEADER = bridge.h
TARGETS := cat
.PHONY : all $(TARGETS)
all: $(TARGETS)
$(TARGETS):
swiftc -sdk $(SDKPATH) $@.swift -import-objc-header $(CBRIDGEHEADER) -o $@
@dduan
dduan / run_tests.py
Created June 28, 2012 22:57
Discover and run all tests in a Python project.
#!/usr/bin/python
''' Runs tests defined in package 'tests' '''
from unittest import defaultTestLoader, TextTestRunner
TextTestRunner().run(defaultTestLoader.discover('tests', pattern='*.py'))
@dduan
dduan / PHP's fucked up syntax.php
Created August 9, 2012 01:38
Nobody in the right mind would ever invent or use this.
// from http://www.php.net/manual/en/language.types.string.php
<?php
// Show all errors.
error_reporting(E_ALL);
class beers {
const softdrink = 'rootbeer';
public static $ale = 'ipa';
}