summaryrefslogtreecommitdiffhomepage
path: root/swift.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'swift.html.markdown')
-rw-r--r--swift.html.markdown130
1 files changed, 105 insertions, 25 deletions
diff --git a/swift.html.markdown b/swift.html.markdown
index 005e511c..ffc57e69 100644
--- a/swift.html.markdown
+++ b/swift.html.markdown
@@ -3,14 +3,15 @@ language: swift
contributors:
- ["Grant Timmerman", "http://github.com/grant"]
- ["Christopher Bess", "http://github.com/cbess"]
+ - ["Joey Huang", "http://github.com/kamidox"]
filename: learnswift.swift
---
-Swift is a programming language for iOS and OS X development created by Apple. Designed to coexist with Objective-C and to be more resilient against erroneous code, Swift was introduced in 2014 at Apple's developer conference WWDC. It is built with the LLVM compiler included in Xcode 6 beta.
+Swift is a programming language for iOS and OS X development created by Apple. Designed to coexist with Objective-C and to be more resilient against erroneous code, Swift was introduced in 2014 at Apple's developer conference WWDC. It is built with the LLVM compiler included in Xcode 6+.
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
@@ -23,7 +24,7 @@ import UIKit
// Xcode supports landmarks to annotate your code and lists them in the jump bar
// MARK: Section mark
// TODO: Do something soon
-// FIXME Fix this code
+// FIXME: Fix this code
println("Hello, world")
@@ -55,8 +56,8 @@ println("Build value: \(buildValue)") // Build value: 7
/*
Optionals are a Swift language feature that allows you to store a `Some` or
`None` value.
-
- Because Swift requires every property to have a value, even nil must be
+
+ Because Swift requires every property to have a value, even nil must be
explicitly stored as an Optional value.
Optional<T> is an enum.
@@ -94,7 +95,8 @@ var anyObjectVar: AnyObject = 7
anyObjectVar = "Changed value to a string, not good practice, but possible."
/*
-Comment here
+ Comment here
+
/*
Nested comments are also supported
*/
@@ -112,8 +114,9 @@ Comment here
// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
-let emptyArray = [String]() // immutable
-var emptyMutableArray = [String]() // mutable
+let emptyArray = [String]() // let == immutable
+let emptyArray2 = Array<String>() // same as above
+var emptyMutableArray = [String]() // var == mutable
// Dictionary
@@ -122,8 +125,9 @@ var occupations = [
"kaylee": "Mechanic"
]
occupations["Jayne"] = "Public Relations"
-let emptyDictionary = [String: Float]() // immutable
-var emptyMutableDictionary = [String: Float]() // mutable
+let emptyDictionary = [String: Float]() // let == immutable
+let emptyDictionary2 = Dictionary<String, Float>() // same as above
+var emptyMutableDictionary = [String: Float]() // var == mutable
//
@@ -165,14 +169,16 @@ do {
} while 1 == 2
// Switch
+// Very powerful, think `if` statements with syntax candy
+// They support String, object instances, and primitives (Int, Double, etc)
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
let vegetableComment = "That would make a good tea sandwich."
-case let x where x.hasSuffix("pepper"):
- let vegetableComment = "Is it a spicy \(x)?"
+case let localScopeValue where localScopeValue.hasSuffix("pepper"):
+ let vegetableComment = "Is it a spicy \(localScopeValue)?"
default: // required (in order to cover all possible input)
let vegetableComment = "Everything tastes good in soup."
}
@@ -186,21 +192,28 @@ default: // required (in order to cover all possible input)
// in functions and can be passed around
// Function with Swift header docs (format as reStructedText)
+
/**
-A greet operation
+ A greet operation
-- A bullet in docs
-- Another bullet in the docs
+ - A bullet in docs
+ - Another bullet in the docs
-:param: name A name
-:param: day A day
-:returns: A string containing the name and day value.
+ :param: name A name
+ :param: day A day
+ :returns: A string containing the name and day value.
*/
func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
+// similar to above except for the function parameter behaviors
+func greet2(#requiredName: String, externalParamName localParamName: String) -> String {
+ return "Hello \(requiredName), the day is \(localParamName)"
+}
+greet2(requiredName:"John", externalParamName: "Sunday")
+
// Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
@@ -281,7 +294,7 @@ print(numbers) // [3, 6, 18]
// Structures and classes have very similar capabilites
struct NamesTable {
- let names: [String]
+ let names = [String]()
// Custom subscript
subscript(index: Int) -> String {
@@ -291,8 +304,8 @@ struct NamesTable {
// Structures have an auto-generated (implicit) designated initializer
let namesTable = NamesTable(names: ["Me", "Them"])
-//let name = namesTable[2]
-//println("Name is \(name)") // Name is Them
+let name = namesTable[1]
+println("Name is \(name)") // Name is Them
//
// MARK: Classes
@@ -341,7 +354,7 @@ internal class Rect: Shape {
init(sideLength: Int) {
self.sideLength = sideLength
- // always super.init last when init custom properties
+ // always super.init last when init custom properties
super.init()
}
@@ -368,11 +381,43 @@ print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4
+// cast instance
+let aShape = mySquare as Shape
+
// compare instances, not the same as == which compares objects (equal to)
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
@@ -393,6 +438,39 @@ enum Suit {
}
}
+// Enum values allow short hand syntax, no need to type the enum type
+// when the variable is explicitly declared
+var suitValue: Suit = .Hearts
+
+// Non-Integer enums require direct raw value assignments
+enum BookName: String {
+ case John = "John"
+ case Luke = "Luke"
+}
+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
@@ -419,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?()
@@ -455,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.
@@ -490,5 +571,4 @@ println(mySquare.sideLength) // 4
// change side length using custom !!! operator, increases size by 3
!!!mySquare
println(mySquare.sideLength) // 12
-
```