Merge pull request #887 from cbess/master

[swift/en] Add more code examples, update some code and comments
This commit is contained in:
Levi Bostian 2014-12-13 20:30:21 -06:00
commit cb2e6b6f99

View File

@ -6,7 +6,7 @@ contributors:
filename: learnswift.swift 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. The official [Swift Programming Language](https://itunes.apple.com/us/book/swift-programming-language/id881256329) book from Apple is now available via iBooks.
@ -23,7 +23,7 @@ import UIKit
// Xcode supports landmarks to annotate your code and lists them in the jump bar // Xcode supports landmarks to annotate your code and lists them in the jump bar
// MARK: Section mark // MARK: Section mark
// TODO: Do something soon // TODO: Do something soon
// FIXME Fix this code // FIXME: Fix this code
println("Hello, world") println("Hello, world")
@ -94,7 +94,8 @@ var anyObjectVar: AnyObject = 7
anyObjectVar = "Changed value to a string, not good practice, but possible." anyObjectVar = "Changed value to a string, not good practice, but possible."
/* /*
Comment here Comment here
/* /*
Nested comments are also supported Nested comments are also supported
*/ */
@ -112,8 +113,9 @@ Comment here
// Array // Array
var shoppingList = ["catfish", "water", "lemons"] var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water" shoppingList[1] = "bottle of water"
let emptyArray = [String]() // immutable let emptyArray = [String]() // let == immutable
var emptyMutableArray = [String]() // mutable let emptyArray2 = Array<String>() // same as above
var emptyMutableArray = [String]() // var == mutable
// Dictionary // Dictionary
@ -122,8 +124,9 @@ var occupations = [
"kaylee": "Mechanic" "kaylee": "Mechanic"
] ]
occupations["Jayne"] = "Public Relations" occupations["Jayne"] = "Public Relations"
let emptyDictionary = [String: Float]() // immutable let emptyDictionary = [String: Float]() // let == immutable
var emptyMutableDictionary = [String: Float]() // mutable let emptyDictionary2 = Dictionary<String, Float>() // same as above
var emptyMutableDictionary = [String: Float]() // var == mutable
// //
@ -165,14 +168,16 @@ do {
} while 1 == 2 } while 1 == 2
// Switch // Switch
// Very powerful, think `if` statements with syntax candy
// They support String, object instances, and primitives (Int, Double, etc)
let vegetable = "red pepper" let vegetable = "red pepper"
switch vegetable { switch vegetable {
case "celery": case "celery":
let vegetableComment = "Add some raisins and make ants on a log." let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress": case "cucumber", "watercress":
let vegetableComment = "That would make a good tea sandwich." let vegetableComment = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"): case let localScopeValue where localScopeValue.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(x)?" let vegetableComment = "Is it a spicy \(localScopeValue)?"
default: // required (in order to cover all possible input) default: // required (in order to cover all possible input)
let vegetableComment = "Everything tastes good in soup." let vegetableComment = "Everything tastes good in soup."
} }
@ -186,21 +191,28 @@ default: // required (in order to cover all possible input)
// in functions and can be passed around // in functions and can be passed around
// Function with Swift header docs (format as reStructedText) // Function with Swift header docs (format as reStructedText)
/** /**
A greet operation A greet operation
- A bullet in docs - A bullet in docs
- Another bullet in the docs - Another bullet in the docs
:param: name A name :param: name A name
:param: day A day :param: day A day
:returns: A string containing the name and day value. :returns: A string containing the name and day value.
*/ */
func greet(name: String, day: String) -> String { func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)." return "Hello \(name), today is \(day)."
} }
greet("Bob", "Tuesday") 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 // Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) { func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79) return (3.59, 3.69, 3.79)
@ -281,7 +293,7 @@ print(numbers) // [3, 6, 18]
// Structures and classes have very similar capabilites // Structures and classes have very similar capabilites
struct NamesTable { struct NamesTable {
let names: [String] let names = [String]()
// Custom subscript // Custom subscript
subscript(index: Int) -> String { subscript(index: Int) -> String {
@ -291,8 +303,8 @@ struct NamesTable {
// Structures have an auto-generated (implicit) designated initializer // Structures have an auto-generated (implicit) designated initializer
let namesTable = NamesTable(names: ["Me", "Them"]) let namesTable = NamesTable(names: ["Me", "Them"])
//let name = namesTable[2] let name = namesTable[1]
//println("Name is \(name)") // Name is Them println("Name is \(name)") // Name is Them
// //
// MARK: Classes // MARK: Classes
@ -368,6 +380,9 @@ print(mySquare.getArea()) // 25
mySquare.shrink() mySquare.shrink()
print(mySquare.sideLength) // 4 print(mySquare.sideLength) // 4
// cast instance
let aShape = mySquare as Shape
// compare instances, not the same as == which compares objects (equal to) // compare instances, not the same as == which compares objects (equal to)
if mySquare === mySquare { if mySquare === mySquare {
println("Yep, it's mySquare") println("Yep, it's mySquare")
@ -393,6 +408,17 @@ 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)")
// //
// MARK: Protocols // MARK: Protocols
@ -490,5 +516,4 @@ println(mySquare.sideLength) // 4
// change side length using custom !!! operator, increases size by 3 // change side length using custom !!! operator, increases size by 3
!!!mySquare !!!mySquare
println(mySquare.sideLength) // 12 println(mySquare.sideLength) // 12
``` ```