diff options
Diffstat (limited to 'swift.html.markdown')
| -rw-r--r-- | swift.html.markdown | 61 | 
1 files changed, 58 insertions, 3 deletions
| diff --git a/swift.html.markdown b/swift.html.markdown index 0d1d2df4..ffc57e69 100644 --- a/swift.html.markdown +++ b/swift.html.markdown @@ -3,6 +3,7 @@ language: swift  contributors:    - ["Grant Timmerman", "http://github.com/grant"]    - ["Christopher Bess", "http://github.com/cbess"] +  - ["Joey Huang", "http://github.com/kamidox"]    filename: learnswift.swift  --- @@ -10,7 +11,7 @@ Swift is a programming language for iOS and OS X development created by Apple. D  The official [Swift Programming Language](https://itunes.apple.com/us/book/swift-programming-language/id881256329) book from Apple is now available via iBooks. -See also Apple's [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/LandingPage/index.html), which has a complete tutorial on Swift. +See also Apple's [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/RoadMapiOS/index.html), which has a complete tutorial on Swift.  ```swift  // import a module @@ -388,6 +389,35 @@ if mySquare === mySquare {      println("Yep, it's mySquare")  } +// Optional init +class Circle: Shape { +    var radius: Int +    override func getArea() -> Int { +        return 3 * radius * radius +    } +     +    // Place a question mark postfix after `init` is an optional init +    // which can return nil +    init?(radius: Int) { +        self.radius = radius +        super.init() +         +        if radius <= 0 { +            return nil +        } +    } +} + +var myCircle = Circle(radius: 1) +println(myCircle?.getArea())    // Optional(3) +println(myCircle!.getArea())    // 3 +var myEmptyCircle = Circle(radius: -1) +println(myEmptyCircle?.getArea())    // "nil" +if let circle = myEmptyCircle { +    // will not execute since myEmptyCircle is nil +    println("circle is not nil") +} +  //  // MARK: Enums @@ -419,6 +449,28 @@ enum BookName: String {  }  println("Name: \(BookName.John.rawValue)") +// Enum with associated Values +enum Furniture { +    // Associate with Int +    case Desk(height: Int) +    // Associate with String and Int +    case Chair(String, Int) +     +    func description() -> String { +        switch self { +        case .Desk(let height): +            return "Desk with \(height) cm" +        case .Chair(let brand, let height): +            return "Chair of \(brand) with \(height) cm" +        } +    } +} + +var desk: Furniture = .Desk(height: 80) +println(desk.description())     // "Desk with 80 cm" +var chair = Furniture.Chair("Foo", 40) +println(chair.description())    // "Chair of Foo with 40 cm" +  //  // MARK: Protocols @@ -445,7 +497,10 @@ class MyShape: Rect {      func grow() {          sideLength += 2 -         + +        // Place a question mark after an optional property, method, or +        // subscript to gracefully ignore a nil value and return nil +        // instead of throwing a runtime error ("optional chaining").          if let allow = self.delegate?.canReshape?() {              // test for delegate then for method              self.delegate?.reshaped?() @@ -481,7 +536,7 @@ extension Int {  }  println(7.customProperty) // "This is 7" -println(14.multiplyBy(2)) // 42 +println(14.multiplyBy(3)) // 42  // Generics: Similar to Java and C#. Use the `where` keyword to specify the  //   requirements of the generics. | 
