Quickies

[categories] [index] [all (531)] [latest]

Swift
  1. if ("ABCDE" as NSString).containsString("BC") {
        println("Containing")
    }
    
  2. let group = dispatch_group_create()
    let mainQueue = dispatch_get_main_queue()
    
    for i in 1...10 {
    
        dispatch_group_enter(hgReadGroup)
    
        longSubTask(param:String, completionHandler: { (results) -> () in
            // ...
            dispatch_group_leave(group)
        })
    }
    
    dispatch_group_notify(group, mainQueue) {
        print("-> all subtasks are finished")
        completionHandler(entries:entries)
    }
    
  3. let date = NSDate()
    let name: AnyClass! = object_getClass(date) // __NSDate
    
    let string = "Hello"
    let name = string.dynamicType // Swift.String
    

    also:

    class MyClass { }
    
    let o : AnyObject = MyClass()
    
    print(o is String) // false
    print(o is MyClass) // true
    
  4. guard let data = optionalData,
       optionalDict = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? [String:AnyObject],
       d = optionalDict,
       latestVersionString = d["latest_version_string"] as? String,
       latestVersionURL = d["latest_version_url"] as? String else {
       return
    }
    
  5. static var x = {
        // ...
        return xxx 
    }()
    
  6. let date = NSDate()
    let unitFlags: NSCalendarUnit = [.Hour, .Day, .Month, .Year]
    let components = NSCalendar.currentCalendar().components(unitFlags, fromDate: date)
    
  7. $ xcrun swift
    Welcome to Swift!  Type :help for assistance.
      1> var x = "toto"
    x: String = "toto"
      2> println(x)
    toto
      3>  
    
  8. private var stravaAPI : StravaAPI = {
        return StravaAPI()
    }()
    
  9. public class Person {
        public private(set) var name: String
        // ...
    }
    
  10. #!/usr/bin/swift
    
    /*
     $ swift x.swift
    */
    
    import Foundation
    
    for arg in Process.arguments {
        print(arg)
    }
    
  11. static let dateFormatter: NSDateFormatter = {
        let df = NSDateFormatter()
        df.dateFormat = "yyyyMMdd"
        return df
    }()
    
  12. struct Board {
    
        var cells : [[Int]]
    
        init(row:Int, columns:Int) {
            self.cells = [[Int]]()
            for _ in 0..<row {
                self.cells.append([Int](count: columns, repeatedValue: 0))
            }
        }
    
        subscript(row: Int, column: Int) -> Int {
            get {
                return cells[row][column]
            }
    
            set {
                cells[row][column] = newValue
            }
        }
    }
    
    var b = Board(row: 3, columns: 4)
    
    b[1,1] = 1
    
    print(b[1,1])
    
  13. #!/usr/bin/env xcrun --toolchain com.apple.dt.toolchain.XcodeDefault swift
    
    // -F/my/frameworks               add to search path
    // -framework MyUtils.framework   load framework
    // -D FLAG                        compilation flag
    
    import Foundation
    
    print(CommandLine.arguments)
    
  14. class MyClass {
        static let sharedInstance = MyClass()
    }