Skip to content

Instantly share code, notes, and snippets.

@dduan
dduan / migrate.py
Created October 25, 2016 00:10
Migrate Xcode 8.0 style comment to Xcode 8.1 style
# usage:
# IFS=$'\n' find ROOT_SOURCE_FOLDER -name "*.swift" -exec python PATH/TO/migrate.py {} \;
import sys
import re
func = re.compile('func\s+\w+\(')
param = re.compile('(?P<indent>\s*)/// -[ ]*[pP]arameter[ ]+(?P<name>\w+)[ ]*:(?P<description>[^\n]+)\n')
ret = re.compile('(?P<indent>\s*)/// -[ ]*[rR]eturns[ ]*:(?P<description>[^\n]+)\n')
continuation = re.compile('\s*///(?P<description>[^\n]+)')
@dduan
dduan / IBInspectableTest.swift
Created November 26, 2016 06:12
Tests for supported IBInspectable types on iOS
import UIKit
class TestView: UIView {
@IBInspectable private var string: String = ""
@IBInspectable private var stringiuo: String!
@IBInspectable private var stringoptional: String?
@IBInspectable private var iuostring: ImplicitlyUnwrappedOptional<String>
@IBInspectable private var optionalstring: Optional<String>
@IBInspectable private var nsstring: NSString = ""
@IBInspectable private var nsstringiuo: NSString!
@dduan
dduan / XCTest+Eventually.swift
Last active January 11, 2023 00:26
A simple "eventually" method to aide asynchronous testing with XCTest
import XCTest
extension XCTestCase {
/// Simple helper for asynchronous testing.
/// Usage in XCTestCase method:
/// func testSomething() {
/// doAsyncThings()
/// eventually {
/// /* XCTAssert goes here... */
@dduan
dduan / withCStrings.swift
Last active August 27, 2018 20:19
A function that turns a Swift 3 [String] to an array of C strings (char *[]).
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
@dduan
dduan / runCommand.swift
Last active June 18, 2025 21:34
How to fork()+execv() in Swift
import Foundation
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
enum RunCommandError: Error {
case WaitPIDError

Remove type inference for stored properties

Introduction

When a type's stored property is declared with a default value, user may choose to let Swift's compiler to automatically deduct type for this property instead of writing it explicitly. We propose this feature be removed from Swift.

@dduan
dduan / bug.swift
Created October 29, 2017 05:21
Enum setter error reporting seems too simplistic?
enum E {
final class P { var v = 0 }
case a(P)
var p: P {
switch self {
case .a(let p): return p
}
}
@dduan
dduan / ADT.swift
Created December 18, 2017 19:27
Representing Algebraic Data Type?
struct Decl {
struct Option {
enum Segment {
case one(String, [String])
indirect case map(Segment, Segment)
}
typealias Field = (name: String?, type: Segment)
let name: String
let fields: [Field]
}
@dduan
dduan / Makefile
Created December 29, 2017 04:13
"Hello, World!" In WebAssembly
compile:
wat2wasm main.wat -o main.wasm
serve:
python -m SimpleHTTPServer
@dduan
dduan / quiet.sh
Last active January 7, 2018 06:34
Run any command and suppress its output to stdin and stderr, unless it returns an error code.
#!/usr/bin/env bash
# Run any command and suppress its output to stdin and stderr, unless it returns an error code.
OUTPUT=$(${@:1} 2>&1)
if [ $? -eq 0 ]; then
exit
fi
OLDLFS=$LFS