Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / IntRandom.swift
Created October 17, 2015 20:37
An extension on Int to generate random Ints and alphanumeric strings.
extension Int {
func asUpperBoundForRandom() -> Int {
return Int(arc4random_uniform(UInt32(self)))
}
func asLengthForRandomAlphanumeric() -> String {
let alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".characters
var chars: [Character] = []
for _ in 1...self {
@dduan
dduan / intersectInPlacePerformance.swift
Created December 12, 2015 02:03
A simple program to benchmark Set.intersectInPlace() in Swift
import Foundation
let smallSet = Set(Array(1..<999).sort {_, _ in arc4random() % 2 == 0})
let largeSet = Set(Array(1..<99999).sort {_, _ in arc4random() % 2 == 0})
var _count = 999999
let unique: () -> Int = { _count += 1; return _count }
var before = NSDate()
var after = NSDate()