Skip to content

Instantly share code, notes, and snippets.

@dduan
dduan / QuickOutline.swift
Last active February 10, 2021 09:50
This script takes a JSON text file with content in {"string:["a","b",…]} format and generates Quick/BDD style test outlines.
#!/usr/bin/env xcrun swift
/*
usage:
1. chmod +x QuickOutline.swift
2. ./QuickOutline.swift outline.json
this will generate BDD/[Quick](https://github.com/Quick/Quick) style test outlines according to outline.json
an example outline.json:
@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 / CaseInsensitiveDictionary.swift
Last active June 18, 2019 11:58
A Dictionary implementation in which the key can be case-insensitive.
// CaseInsensitiveDictionary.swift
// Created by Daniel Duan on 12/19/14.
// Usaege Example:
// var test = CaseInsensitiveDictionary<String, Int>()
// test["Winter is coming"] = 1
//
// test["WINTER is Coming"] = test["Winter Is Coming"] // true
// test["Hear Our Roar?"] = 1
//
// test.count == 2 // true
@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 / 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 / UISegmentedControl+VerticalLayout.swift
Last active April 19, 2023 14:50
Turns a UISegmentedControl into a vertical layout.
import UIKit
extension UISegmentedControl {
func goVertical() {
self.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
for segment in self.subviews {
for segmentSubview in segment.subviews {
if segmentSubview is UILabel {
(segmentSubview as UILabel).transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
@dduan
dduan / NSAttributedString+SimpleHTMLTag.swift
Created December 7, 2014 05:59
Convert Simple Text With HTML Tags to NSAttributedString
extension NSAttributedString {
func replaceHTMLTag(tag: String, withAttributes attributes: [String: AnyObject]) -> NSAttributedString {
let openTag = "<\(tag)>"
let closeTag = "</\(tag)>"
let resultingText: NSMutableAttributedString = self.mutableCopy() as NSMutableAttributedString
while true {
let plainString = resultingText.string as NSString
let openTagRange = plainString.rangeOfString(openTag)
if openTagRange.length == 0 {
Verifying myself: My Bitcoin username is +duan. https://onename.io/duan
@dduan
dduan / gist:2ecc257ab908aff24530
Created July 7, 2014 02:34
Guessing MIME type from a file name in Objective-C on iOS
- (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
// Borrowed from http://stackoverflow.com/questions/2439020/wheres-the-iphone-mime-type-database
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[fileName pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
return (__bridge NSString *)(MIMEType);
}

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: