Skip to content

Instantly share code, notes, and snippets.

import Foundation
class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
}
struct Notification<A> {
let name: String
}
// Playground - noun: a place where people can play
import Foundation
import UIKit
//~~~~~~~~~~~~~~~~ Error handling with enum Result<A> ~~~~~~~
enum Result<A> {
case Error(NSError)
case Value(Box<A>)
import Foundation
class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
}
struct Notification<A> {
let name: String
}
//
// ISO3661-2alpha2.swift
// ISO3661-2-alpha2
//
// Created by Bernd Rabe on 01.04.15.
// Copyright (c) 2015 Bernd Rabe. All rights reserved.
//
import UIKit
import Foundation
extension String
{
var length: Int {
get {
return countElements(self)
}
}
// Helper function to convert from RGB to UIColor
// UIColorFromRGB(0x22B573)
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}

Swift Don'ts

  • Don't add code cruft. Avoid parentheses around conditions in if-statements or with the return keyword. Don't add semicolons except where syntactically demanded in statements or to separate statements on the same line.
  • Don't use ALL_CAPS; use camelCase
  • Don't fight type inference. Use enumeration prefixes, self-references, and class names (with constructors) only when necessary or to clarify coding intent.
  • Don't use var when let is appropriate, especially for properties. The compiler better optimizes let statements for items whose values will not change during their lifetime. For example, Apple writes, "It is good practice to create immutable collections in all cases where the collection does not need to change. Doing so enables the Swift compiler to optimize the performance of the collections you create."
  • Don't use classes when structs will do. Use class
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
@zhjuncai
zhjuncai / StringSize.swift
Last active September 20, 2015 15:41 — forked from plumhead/StringSize.swift
String extension to find the layout size of a String with specified attributes.
extension String {
func size(withAttributes attrs: [String:AnyObject], constrainedTo box: NSSize) -> NSRect {
let storage = NSTextStorage(string: self)
let container = NSTextContainer(containerSize: NSSize(width: box.width, height: box.height))
let layout = NSLayoutManager()
layout.addTextContainer(container)
storage.addLayoutManager(layout)
storage.addAttributes(attrs, range: NSMakeRange(0, storage.length))
container.lineFragmentPadding = 0.0
let _ = layout.glyphRangeForTextContainer(container)
@zhjuncai
zhjuncai / gist:5797427
Created June 17, 2013 14:46
Mircoformat
<div class="vcard">
<div class="fn org">Wikimedia Foundation Inc.</div>
<div class="adr">
<div class="street-address">149 New Montgomery Street, 3rd Floor</div>
<div> <span class="locality">San Francisco</span>, <abbr class="region" title="California">CA</abbr> <span class="postal-code">94105</span></div>
<div class="country-name">USA</div>
</div>
<div>Phone: <span class="tel">+1-415-839-6885</span></div>
<div>Email: <span class="email">info@wikimedia.org</span></div>
<div>