Apple/Swift

Un article de Agora2ia.

< Apple(Redirigé depuis Langages/Apple/Swift)


Sommaire

Caractéristiques

  • Design
    • Closures unified with function pointers
    • Tuples and multiple return values
    • Generics
    • Fast and concise iteration over a range or collection
    • Structs that support methods, extensions, protocols.
    • Functional programming patterns, e.g.: map and filter
    • Other properties:
      • Types: class, protocole, extension and enum
      • Equality (==, value) and Identity (===, reference) Operators
      • typealias (typealias AudioSample = UInt16; var maxAmplitudeFound = AudioSample.min)
      • /Optionals (Optional Binding, Implicitly Unwrapped Optionals)
  • /REPL
  • Absence d'/Exception
  • Interactive Playgrounds


Topics

Snippets


The Language

Variables and constants

  • var myVariable :Int = 7 // Variable
  • let MagazineEntityName = "Magazine" // Constante
  • static let MagazineEntityName = "Magazine" // Constante statique (si définie comme membre d'une classe on peut alors l'appeler MyClass.MyConstante)


Collections

Sous-pages

Apple/Swift/DateApple/Swift/ExceptionApple/Swift/InteractivePlaygrounds
Apple/Swift/MethodsApple/Swift/ObjectiveCMappingApple/Swift/Optionals
Apple/Swift/REPLApple/Swift/RandomApple/Swift/Selector
Apple/Swift/SnippetsApple/Swift/Snippets/BuildNumberApple/Swift/Snippets/Flurry
Apple/Swift/Snippets/UIWebViewApple/Swift/UITableViewController


Ressources

Apple, The Swift Programming Language


Autre


Coding


Code encyclopédique

class Country {
   let name: String
   let capitalCity: City!
   init(name: String, capitalName: String) {
       self.name = name
       self.capitalCity = City(name: capitalName, country: self)
   }

   // Read-Only Computed Properties
   var area: Double {
       return width * height
   }
}
 
class City {
   let name: String
   unowned let country: Country
   init(name: String, country: Country) {
       self.name = name
       self.country = country
   }

   // Computed Properties
   var center: Point {
       get {
           let centerX = origin.x + (size.width / 2)
           let centerY = origin.y + (size.height / 2)
           return Point(x: centerX, y: centerY)
       }
       set(newCenter) {
           origin.x = newCenter.x - (size.width / 2)
           origin.y = newCenter.y - (size.height / 2)
       }
   }

   // Property Observers
   var totalSteps: Int = 0 {
       willSet(newTotalSteps) {
           println("About to set totalSteps to \(newTotalSteps)")
       }
       didSet {
           if totalSteps > oldValue  {
               println("Added \(totalSteps - oldValue) steps")
           }
       }
   }

   func addTwoInts(a: Int, b: Int) -> Int {
       return a + b
   }
}

// Tuple
let (statusCode, statusMessage) = http404Error;
// statusCode == http404Error.0 == http404Error.statusCode

// Optional Binding
if let actualNumber = possibleNumber.toInt() {
   println("\'\(possibleNumber)\' has an integer value of \(actualNumber)")
} else {
   println("\'\(possibleNumber)\' could not be converted to an integer")
}