Skip to content

Instantly share code, notes, and snippets.

function buildTemplate (spec, template) {
return function (context) {
var allParamsExist = spec.every(function (key) {
return context.hasOwnProperty(key) && (context[key] !== undefined)
})
if (!allParamsExist) {
throw "Context object misses needed parameters"
}
import UIKit
extension String {
func NSRangeOfString(string: String) -> NSRange? {
if let range = self.rangeOfString(string) {
let loc = distance(self.startIndex, range.startIndex)
let length = distance(range.startIndex, range.endIndex)
return NSMakeRange(loc, length)
// Dein fehler: '[AnyObject]?' is not a subtype of 'Void'
// dismissViewControllerAnimated will eine Closure mit der Typ-Signatur () -> Void.
// Wenn die Closure nur aus einer Expression besteht, wie hier der Aufruf von popToViewController
// dann wird der Return Value dieser Expression automagisch als Return der Closure angenommen.
// popToViewController gibt [AnyObject]? zurück. Das ist nicht Void.
// Wie fixen? So sollte es gehen.
dismissViewControllerAnimated(true, completion: {
class Bookmark {}
class WebView {}
class Something {}
var bookmark : Bookmark? = Bookmark()
var webview : WebView? = WebView()
var something : Something? = nil
infix operator &&! {}
@ttepasse
ttepasse / gist:1c6dbc225aed56eec744
Last active August 29, 2015 14:11
Optionals with boolean values
// The following would be great, if not for this error message
// “Extension of generic type Optional<T> from a different module cannot provide public declarations”
//
// ... which seems to be a bug in Swift:
// https://twitter.com/jckarter/status/513429427179356161
extension Optional : BooleanType {
public var boolValue : Bool {
get {
// I find no better version than manually writing unwrap 1...n
func unwrap <T> (first: T?, closure: (T)->()) -> () {
if let p1 = first {
closure(p1)
}
}
func unwrap <T1, T2> (first: T1?, second: T2?, closure: (T1, T2)->()) -> () {
func uglyFirstLetterTransformation(str: String) -> String? {
if str.isEmpty {
return .None
}
let Letters = NSCharacterSet.letterCharacterSet()
let Numbers = NSCharacterSet.decimalDigitCharacterSet()
let firstLetter : NSString = str.substringToIndex( advance(str.startIndex, 1) )
import UIKit
class FormattableAttributedString {
/* class */ let formatPattern = NSRegularExpression(pattern: "\\{(\\w+)\\}", options: nil, error: nil)!
var formatString : NSMutableAttributedString
var formatSpecifiers : [String: NSRange] = [:]

Der Tabulator auf Schreibmaschinen springt von Spalte zu Spalte auf dem Papier. Das ist praktisch der „nächste Ort der Texteingabe“. Insofern finde ich das vergleichbare Verhalten von Formularfeldern in GUIs (und dadurch auch die Formulare im Browser) eigentlich in sich sinnig.

Aber wer hat's erfunden? In Wikipedia steht:

In many graphical applications, especially on Windows, the Tab key will move the focus to every control or widget such as buttons so that the user interface can be used without a mouse at all (this was part of the IBM Common User Access design).

IBMs Common User Access ist scheiße zu googeln und vermutlich auch zu alt fürs Web. Schließlich war's für GUIs von Wordperfect auf alten DOS oder so gedacht. Aber in manchen Dokumenten wie diesem hier (PDF) findet man es noch. (Cmd+F nach „Tab Key“).

CUA soll von App

import UIKit
let cal = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
let now = NSDate()
var firstDay = NSDateComponents()
firstDay.day = 1
cal.nextDateAfterDate(now, matchingComponents: firstDay, options: NSCalendarOptions.SearchBackwards)