NSTimer is a great example of an over-verbose, outdated Objective-C API. To run a simple line of code after a delay, you need to write a lot of boilerplate crap.
How about this:
NSTimer.schedule(5.seconds) {
println("Hello world!")
}| //If you have a Bridging-Header: | |
| #import <FBSDKCoreKit/FBSDKCoreKit.h> | |
| #import <FBSDKLoginKit/FBSDKLoginKit.h> | |
| //In your AppDelegate: | |
| func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [String: AnyObject]?) -> Bool { | |
| //App launch code | |
| FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) | |
| //Optionally add to ensure your credentials are valid: |
| // (c) 2014 Nate Cook, licensed under the MIT license | |
| // | |
| // Fisher-Yates shuffle as top-level functions and array extension methods | |
| /// Shuffle the elements of `list`. | |
| func shuffle<C: MutableCollectionType where C.Index == Int>(inout list: C) { | |
| let c = count(list) | |
| for i in 0..<(c - 1) { | |
| let j = Int(arc4random_uniform(UInt32(c - i))) + i | |
| swap(&list[i], &list[j]) |
| #!/bin/sh | |
| UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal | |
| # make sure the output directory exists | |
| mkdir -p "${UNIVERSAL_OUTPUTFOLDER}" | |
| # Step 1. Build Device and Simulator versions | |
| xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build | |
| xcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build |
| /* | |
| 모나드는 특정한 타입을 감싸는 타입이며, | |
| raw한 값을 감싸는 함수와 | |
| raw한 값을 모나드 값으로 바꾸는 어떤 함수에 바인딩된다. | |
| 이를 바탕으로 모나드 프로토콜을 정의하면 다음과 같다. | |
| */ | |
| protocol Monad { |
NSTimer is a great example of an over-verbose, outdated Objective-C API. To run a simple line of code after a delay, you need to write a lot of boilerplate crap.
How about this:
NSTimer.schedule(5.seconds) {
println("Hello world!")
}| func encode<T>(var value: T) -> NSData { | |
| return withUnsafePointer(&value) { p in | |
| NSData(bytes: p, length: sizeofValue(value)) | |
| } | |
| } | |
| func decode<T>(data: NSData) -> T { | |
| let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T.Type)) | |
| data.getBytes(pointer) | |
| class Regex { | |
| let pattern: String | |
| let options: NSRegularExpressionOptions! | |
| private var matcher: NSRegularExpression { | |
| return NSRegularExpression(pattern: self.pattern, options: nil, error: nil) | |
| } | |
| required init(pattern: String, options: NSRegularExpressionOptions = nil) { | |
| self.pattern = pattern |
| extension Dictionary { | |
| init (_ array: Array<Element>) { | |
| self = [ : ] | |
| self.merge(array) | |
| } | |
| mutating func merge (array: Array<Element>) { | |
| for (key: KeyType, value: ValueType) in array { | |
| self[key] = value | |
| } |
| import Foundation | |
| struct Meter { | |
| var value: Double | |
| init(_ value: Double) { | |
| self.value = value | |
| } | |
| var mm: Double { return value * 1000.0 } |
| import UIKit | |
| // How to make singleton classes in Swift: http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift | |
| // Basically using global constants is the most easy and safe way to go | |
| class APIClient: NSObject { | |
| let functionSessionManager:AFHTTPSessionManager | |
| class var sharedInstance:APIClient { |