- 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
whenlet
is appropriate, especially for properties. The compiler better optimizeslet
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MultiDirectionAdjudicatingScrollView.swift | |
// Khan Academy | |
// | |
// Created by Andy Matuschak on 12/16/14. | |
// Copyright (c) 2014 Khan Academy. All rights reserved. | |
// | |
import UIKit | |
import UIKit.UIGestureRecognizerSubclass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Dictionary; | |
if (!Dictionary) { | |
Dictionary = {}; | |
} | |
(function () { | |
'use strict'; | |
function f(n) { | |
// Format integers to have at least two digits. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define COMPARE(A, B) ({ \ | |
__typeof__(A) __a = (A); \ | |
__typeof__(B) __b = (B); \ | |
__a < __b ? NSOrderedAscending : __a == __b ? NSOrderedSame : NSOrderedDescending; \ | |
}) |
See this SO discussion.
- https://github.com/simonwhitaker/swmath — define new functions which utilize clang function overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import UIKit | |
var str = "Hello, playground" | |
enum TimeIntervalUnit { | |
case Seconds, Minutes, Hours, Days, Months, Years | |
func dateComponents(interval: Int) -> NSDateComponents { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys; reload(sys); sys.setdefaultencoding('utf-8') | |
import sqlite3 | |
import urllib2 | |
import urllib | |
import simplejson | |
login = 'your github id' | |
token = 'your github api key' | |
repo = 'username/reponame' |
NewerOlder