mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
- update Swift examples
- update to upgrade to Swift 2.1 - code cleanup
This commit is contained in:
parent
df0992d72c
commit
618f8f5bad
@ -32,7 +32,7 @@ import UIKit
|
|||||||
|
|
||||||
// In Swift 2, println and print were combined into one print method. Print automatically appends a new line.
|
// In Swift 2, println and print were combined into one print method. Print automatically appends a new line.
|
||||||
print("Hello, world") // println is now print
|
print("Hello, world") // println is now print
|
||||||
print("Hello, world", appendNewLine: false) // printing without appending a newline
|
print("Hello, world", terminator: "") // printing without appending a newline
|
||||||
|
|
||||||
// variables (var) value can change after being set
|
// variables (var) value can change after being set
|
||||||
// constants (let) value can NOT be changed after being set
|
// constants (let) value can NOT be changed after being set
|
||||||
@ -178,8 +178,8 @@ while i < 1000 {
|
|||||||
i *= 2
|
i *= 2
|
||||||
}
|
}
|
||||||
|
|
||||||
// do-while loop
|
// repeat-while loop
|
||||||
do {
|
repeat {
|
||||||
print("hello")
|
print("hello")
|
||||||
} while 1 == 2
|
} while 1 == 2
|
||||||
|
|
||||||
@ -221,10 +221,10 @@ default: // required (in order to cover all possible input)
|
|||||||
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", day: "Tuesday")
|
||||||
|
|
||||||
// similar to above except for the function parameter behaviors
|
// similar to above except for the function parameter behaviors
|
||||||
func greet2(#requiredName: String, externalParamName localParamName: String) -> String {
|
func greet2(requiredName requiredName: String, externalParamName localParamName: String) -> String {
|
||||||
return "Hello \(requiredName), the day is \(localParamName)"
|
return "Hello \(requiredName), the day is \(localParamName)"
|
||||||
}
|
}
|
||||||
greet2(requiredName: "John", externalParamName: "Sunday")
|
greet2(requiredName: "John", externalParamName: "Sunday")
|
||||||
@ -247,14 +247,14 @@ func getGasPrices2() -> (lowestPrice: Double, highestPrice: Double, midPrice: Do
|
|||||||
let pricesTuple2 = getGasPrices2()
|
let pricesTuple2 = getGasPrices2()
|
||||||
let price2 = pricesTuple2.lowestPrice
|
let price2 = pricesTuple2.lowestPrice
|
||||||
let (_, price3, _) = pricesTuple2
|
let (_, price3, _) = pricesTuple2
|
||||||
println(pricesTuple2.highestPrice == pricesTuple2.1) // true
|
print(pricesTuple2.highestPrice == pricesTuple2.1) // true
|
||||||
println("Highest gas price: \(pricesTuple2.highestPrice)")
|
print("Highest gas price: \(pricesTuple2.highestPrice)")
|
||||||
|
|
||||||
// Variadic Args
|
// Variadic Args
|
||||||
func setup(numbers: Int...) {
|
func setup(numbers: Int...) {
|
||||||
// its an array
|
// its an array
|
||||||
let number = numbers[0]
|
let _ = numbers[0]
|
||||||
let argCount = numbers.count
|
let _ = numbers.count
|
||||||
}
|
}
|
||||||
|
|
||||||
// Passing and returning functions
|
// Passing and returning functions
|
||||||
@ -275,7 +275,7 @@ func swapTwoInts(inout a: Int, inout b: Int) {
|
|||||||
}
|
}
|
||||||
var someIntA = 7
|
var someIntA = 7
|
||||||
var someIntB = 3
|
var someIntB = 3
|
||||||
swapTwoInts(&someIntA, &someIntB)
|
swapTwoInts(&someIntA, b: &someIntB)
|
||||||
print(someIntB) // 7
|
print(someIntB) // 7
|
||||||
|
|
||||||
|
|
||||||
@ -303,23 +303,17 @@ numbers = numbers.map({ number in 3 * number })
|
|||||||
print(numbers) // [3, 6, 18]
|
print(numbers) // [3, 6, 18]
|
||||||
|
|
||||||
// Trailing closure
|
// Trailing closure
|
||||||
numbers = sorted(numbers) { $0 > $1 }
|
numbers = numbers.sort { $0 > $1 }
|
||||||
|
|
||||||
print(numbers) // [18, 6, 3]
|
print(numbers) // [18, 6, 3]
|
||||||
|
|
||||||
// Super shorthand, since the < operator infers the types
|
|
||||||
|
|
||||||
numbers = sorted(numbers, < )
|
|
||||||
|
|
||||||
print(numbers) // [3, 6, 18]
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// MARK: Structures
|
// MARK: Structures
|
||||||
//
|
//
|
||||||
|
|
||||||
// Structures and classes have very similar capabilities
|
// Structures and classes have very similar capabilities
|
||||||
struct NamesTable {
|
struct NamesTable {
|
||||||
let names = [String]()
|
let names: [String]
|
||||||
|
|
||||||
// Custom subscript
|
// Custom subscript
|
||||||
subscript(index: Int) -> String {
|
subscript(index: Int) -> String {
|
||||||
@ -472,9 +466,10 @@ enum Suit {
|
|||||||
// when the variable is explicitly declared
|
// when the variable is explicitly declared
|
||||||
var suitValue: Suit = .Hearts
|
var suitValue: Suit = .Hearts
|
||||||
|
|
||||||
// Non-Integer enums require direct raw value assignments
|
// String enums can have direct raw value assignments
|
||||||
|
// or their raw values will be derived from the Enum field
|
||||||
enum BookName: String {
|
enum BookName: String {
|
||||||
case John = "John"
|
case John
|
||||||
case Luke = "Luke"
|
case Luke = "Luke"
|
||||||
}
|
}
|
||||||
print("Name: \(BookName.John.rawValue)")
|
print("Name: \(BookName.John.rawValue)")
|
||||||
@ -518,7 +513,7 @@ protocol ShapeGenerator {
|
|||||||
// Protocols declared with @objc allow optional functions,
|
// Protocols declared with @objc allow optional functions,
|
||||||
// which allow you to check for conformance
|
// which allow you to check for conformance
|
||||||
@objc protocol TransformShape {
|
@objc protocol TransformShape {
|
||||||
optional func reshaped()
|
optional func reshape()
|
||||||
optional func canReshape() -> Bool
|
optional func canReshape() -> Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -531,9 +526,9 @@ class MyShape: Rect {
|
|||||||
// Place a question mark after an optional property, method, or
|
// Place a question mark after an optional property, method, or
|
||||||
// subscript to gracefully ignore a nil value and return nil
|
// subscript to gracefully ignore a nil value and return nil
|
||||||
// instead of throwing a runtime error ("optional chaining").
|
// instead of throwing a runtime error ("optional chaining").
|
||||||
if let allow = self.delegate?.canReshape?() {
|
if let reshape = self.delegate?.canReshape?() where reshape {
|
||||||
// test for delegate then for method
|
// test for delegate then for method
|
||||||
self.delegate?.reshaped?()
|
self.delegate?.reshape?()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -546,7 +541,7 @@ class MyShape: Rect {
|
|||||||
// `extension`s: Add extra functionality to an already existing type
|
// `extension`s: Add extra functionality to an already existing type
|
||||||
|
|
||||||
// Square now "conforms" to the `Printable` protocol
|
// Square now "conforms" to the `Printable` protocol
|
||||||
extension Square: Printable {
|
extension Square: CustomStringConvertible {
|
||||||
var description: String {
|
var description: String {
|
||||||
return "Area: \(self.getArea()) - ID: \(self.identifier)"
|
return "Area: \(self.getArea()) - ID: \(self.identifier)"
|
||||||
}
|
}
|
||||||
@ -571,8 +566,8 @@ print(14.multiplyBy(3)) // 42
|
|||||||
// Generics: Similar to Java and C#. Use the `where` keyword to specify the
|
// Generics: Similar to Java and C#. Use the `where` keyword to specify the
|
||||||
// requirements of the generics.
|
// requirements of the generics.
|
||||||
|
|
||||||
func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
|
func findIndex<T: Equatable>(array: [T], _ valueToFind: T) -> Int? {
|
||||||
for (index, value) in enumerate(array) {
|
for (index, value) in array.enumerate() {
|
||||||
if value == valueToFind {
|
if value == valueToFind {
|
||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user