Skip to content

Instantly share code, notes, and snippets.

View sammoore's full-sized avatar
👩‍🍳
cookin'

Sam Moore sammoore

👩‍🍳
cookin'
View GitHub Profile
@sammoore
sammoore / .gitignore
Created October 4, 2015 23:50
iOS .gitignore
# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
@sammoore
sammoore / ReverseAsync.swift
Created July 28, 2016 19:05
ReverseAsync.swift
func ReverseAsync<ReturnType>() -> (completionHandler: ReturnType -> Void, complete: () -> ReturnType) {
var value: Optional<ReturnType> = .None
let semaphore: dispatch_semaphore_t = dispatch_semaphore_create(0)
return (
completionHandler: { v in
value = .Some(v)
dispatch_semaphore_signal(semaphore)
},
complete: {
@sammoore
sammoore / NSStringEncodingExtensions.swift
Created July 29, 2016 20:53
Conversion helpers when handling IANACharSets.
extension NSString {
static func ConvertIANACharSetNameToEncoding(name: String) -> NSStringEncoding? {
let cf_encoding = CFStringConvertIANACharSetNameToEncoding(name)
guard cf_encoding != kCFStringEncodingInvalidId else {
return nil
}
return CFStringConvertEncodingToNSStringEncoding(cf_encoding)
}
@sammoore
sammoore / AltAsOperators.swift
Created August 2, 2016 19:50
Playing with custom operators by adding syntactic sugar / alternative cast operators.
prefix operator !! {}
prefix operator ?? {}
prefix func !!<T, U>(rhs: T) -> U {
return rhs as! U
}
prefix func !!<T, U>(rhs: T) -> U! {
return rhs as! U
}
prefix func ??<T, U>(rhs: T) -> U? {
@sammoore
sammoore / BridgedSwiftStringByAppendingPaths.swift
Created August 3, 2016 19:50
A version of NSString's stringByAppendingPathComponent that's readily available on Swift.String. This implementation uses Foundation (hence the Bridged file name). This
import Swift
import Foundation
extension String {
/// WARNING: Unless `paths.isEmpty`, the resultant String is always backed by NSString storage.
init(basePath: String, paths: [String]) {
guard paths.count > 0 else {
self = basePath; return
}
@sammoore
sammoore / GenericTupleParameterCoercing.swift
Last active August 18, 2016 01:24
Tuples in Swift can usually be directly used as parameters to a function or initializer. It seems that when the parameters to the given function are generics, the compiler can no longer coerce the tuple as parameters. Tested with Swift 2.2 / Xcode 7.3.
// Tested using Swift 2.2 / Xcode 7.3
import Swift
// A function and initializer with 2 parameters of explicit types.
func stringTuple(_ lhs: String, _ rhs: String) {
print("\(lhs), \(rhs)")
}
struct StringTuple {
init(_ lhs: String, _ rhs: String) {
@sammoore
sammoore / ObjCBridgeable.swift
Created September 9, 2016 20:40
A (simpler) protocol to implement than _ObjectiveCBridgeable which implicitly implements it. Tested with Swift 2.2, Xcode 7.3.1. SourceKit tends to have problems parsing this (it will cause crashes) -- edits welcome, although I haven't taken a look in a bit.
import protocol Foundation.NSObjectProtocol
/// The minimum requirements for bridging conforming Swift "named types"
/// into a representable Objective-C type, i.e. a subclass of NSObject.
///
/// Note that implementing the direct descendents of `ObjCBridgeable`
/// provides automatic conformance to `_ObjectiveCBridgeable`.
protocol ObjCBridgeable: _ObjectiveCBridgeable {
// Provides code-completion since the latter is hidden, and provides
@sammoore
sammoore / SMPRNG.js
Last active September 21, 2016 21:47
(BROKEN) A basic PRNG for client-side / web pages, which either resolves the RandomSource / Crypto object from the global Window, otherwise uses a sketchy, cryptographically insecure generator based on Math.random. Do not use this unless you don't need cryptographically secure randomness.
// Sam's PRNG "proxy" for client-side JS, which generates random `UInt32`'s.
// Attempts to resolve the window's Crypto object and use its getRandomValues
// function if available, otherwise provides a cryptographically insecure PRN
// based on combining the current time and Math.random().
//
// Note: if any truthy argument is provided, the Fallback impl. will be used.
// otherwise, attempts to use the Window's Crypto object if possible.
// Usage:
/// var prng = SMPRNG();
/// var uint = prng.next();
@sammoore
sammoore / GenNib.swift
Created September 27, 2016 14:51
Untested Generic Nib Loader
extension UINib {
static func Build<View: UIView>(nibName: String? = nil, bundle: NSBundle? = nil) -> View {
let typeName = String(Mirror(reflecting: View).subjectType)
let nibName: String = nibName ?? typeName
let nib = UINib(nibName: nibName, bundle: bundle)
for object in nib.instantiateWithOwner(nil, options: nil) {
if let view = object as? View {
return view
@sammoore
sammoore / reset.css
Last active October 7, 2017 19:16
yet another reset.css
* {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 62.5%;
vertical-align: baseline;