Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift

 Swift

Swift has some of the best features of other languages. These are my thoughts on that after my initial review of the language.

boundsj

June 20, 2014
Tweet

More Decks by boundsj

Other Decks in Programming

Transcript

  1. for i, v in enumerate(['tic', 'tac', ‘toe’]): print i, v

    https://docs.python.org/2/tutorial/datastructures.html#looping-techniques
  2. void Swap<T>(ref T lhs, ref T rhs) { T temp

    = lhs; lhs = rhs; rhs = temp; } http://msdn.microsoft.com/en-us/library/twcad0zb.aspx
  3. func swap<T>(inout lhs: T, inout rhs: T) { let temp

    = a a = b b = temp } https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Generics.html
  4. T is inferred to be int and string correctly: !

    var someInt = 3 var anotherInt = 107 swap(&someInt, &anotherInt) // someInt is now 107, and anotherInt is now 3 var someString = "hello" var anotherString = "world" swap(&someString, &anotherString) // someString is now "world", // and anotherString is now "hello"
  5. ! var gameOptions = [“tic”, “tac”, “toe”] ! for (index,

    item) in enumerate(gameOptions) { println(“\(index) : \(item)”) } generics in collections
  6. generics in collections ! var gameOptions:String[] = [“tic”, “tac”, “toe”]

    ! for (index, item) in enumerate(gameOptions) { println(“\(index) : \(item)”) }
  7. generics in collections ! var gameOptions:String[] = [“tic”, “tac”, 42]

    ! for (index, item) in enumerate(gameOptions) { println(“\(index) : \(item)”) }
  8. public override int Area() { return side * side; }

    http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
  9. // this class is twice a big! override func area()

    -> Int { return side * side * 2; } https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Inheritance.html
  10. optionals can be evaluated as bools (LogicValue) in conditionals var

    i:Int? = nil if (i) { println(“thank god for logic values”) }
  11. // objective-c view controller @property (strong, nonatomic) AAsyncLib *awesomeAsyncLib; !

    - (IBAction)awesomeButtonPressed:(id)sender { [self.awesomeAsyncLib doIt:^(NSString *result) { [self showResult:result]; }]; }
  12. // objective-c view controller @property (strong, nonatomic) AAsyncLib *awesomeAsyncLib; !

    - (IBAction)awesomeButtonPressed:(id)sender { // remember to avoid retain cycle __weak typeof(self)weakSelf = self; ! [self.awesomeAsyncLib doIt:^(NSString *result) { [weakSelf showResult:result]; }]; }
  13. // objective-c view controller var awesomeAsyncLib: AAsyncLib ! @IBAction func

    awesomeButtonPressed:(sender: NSButton) { awesomeAsyncLib.doIt() { // use capture list to avoid retain cycle // use unowned if value can never be nil // use weak if value can possibly be nil (if you need // to check for existence) [unowned self](result: NSString) in [self.showResult(result)]; } } https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/ AutomaticReferenceCounting.html
  14. resources ! hacker news: https://hn.algolia.com/?q=swift#!/story/forever/0/swift ! code migration: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/ buildingcocoaapps/Migration.html

    ! objc interop: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/ buildingcocoaapps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-XID_26 ! example app: https://developer.apple.com/library/prerelease/ios/samplecode/Lister-Swift/Introduction/ Intro.html