mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
- add more examples; update examples
- now runs in the Xcode 6 b6 playground - add MARK sections
This commit is contained in:
parent
83b63aab54
commit
328a4f1bab
@ -2,6 +2,7 @@
|
||||
language: swift
|
||||
contributors:
|
||||
- ["Grant Timmerman", "http://github.com/grant"]
|
||||
- ["Christopher Bess", "http://github.com/cbess"]
|
||||
filename: learnswift.swift
|
||||
---
|
||||
|
||||
@ -11,21 +12,34 @@ See also Apple's [getting started guide](https://developer.apple.com/library/pre
|
||||
|
||||
```js
|
||||
//
|
||||
// Basics
|
||||
// MARK: Basics
|
||||
//
|
||||
|
||||
println("Hello, world")
|
||||
|
||||
var myVariable = 42
|
||||
//let fƒ∆ = "value" // unicode in variable names
|
||||
let myConstant = 3.1415926
|
||||
let convenience = "keyword" // contextual variable name
|
||||
let weak = "keyword"; let override = "another keyword" // statements can be separated by a semi-colon
|
||||
let `class` = "keyword" // backticks allow keywords to be used as variable names
|
||||
let explicitDouble: Double = 70
|
||||
let intValue = 0007 // 7
|
||||
let largeIntValue = 77_000 // 77000
|
||||
let label = "some text " + String(myVariable) // Casting
|
||||
let piText = "Pi = \(myConstant)" // String interpolation
|
||||
var optionalString: String? = "optional" // Can be nil
|
||||
optionalString = nil
|
||||
|
||||
/*
|
||||
Comment here
|
||||
/*
|
||||
Nested comment here
|
||||
*/
|
||||
*/
|
||||
|
||||
//
|
||||
// Arrays and Dictionaries
|
||||
// MARK: Collections
|
||||
//
|
||||
|
||||
// Array
|
||||
@ -43,7 +57,7 @@ let emptyDictionary = Dictionary<String, Float>()
|
||||
|
||||
|
||||
//
|
||||
// Control Flow
|
||||
// MARK: Control Flow
|
||||
//
|
||||
|
||||
// for loop (array)
|
||||
@ -57,6 +71,7 @@ for value in myArray {
|
||||
}
|
||||
|
||||
// for loop (dictionary)
|
||||
var dict = ["one": 1, "two": 2]
|
||||
for (key, value) in dict {
|
||||
println("\(key): \(value)")
|
||||
}
|
||||
@ -93,7 +108,7 @@ default: // required (in order to cover all possible input)
|
||||
|
||||
|
||||
//
|
||||
// Functions
|
||||
// MARK: Functions
|
||||
//
|
||||
|
||||
// Functions are a first-class type, meaning they can be nested
|
||||
@ -110,7 +125,7 @@ func getGasPrices() -> (Double, Double, Double) {
|
||||
return (3.59, 3.69, 3.79)
|
||||
}
|
||||
|
||||
// Args
|
||||
// Variadic Args
|
||||
func setup(numbers: Int...) {}
|
||||
|
||||
// Passing and returning functions
|
||||
@ -125,7 +140,7 @@ increment(7)
|
||||
|
||||
|
||||
//
|
||||
// Closures
|
||||
// MARK: Closures
|
||||
//
|
||||
var numbers = [1, 2, 6]
|
||||
|
||||
@ -147,11 +162,27 @@ numbers = numbers.map({ number in 3 * number })
|
||||
|
||||
print(numbers) // [3, 6, 18]
|
||||
|
||||
// Trailing closure
|
||||
numbers = sorted(numbers) { $0 > $1 }
|
||||
|
||||
print(numbers) // [18, 6, 3]
|
||||
|
||||
// Super shorthand, since the < operator infers the types
|
||||
|
||||
numbers = sorted(numbers, < )
|
||||
|
||||
print(numbers) // [3, 6, 18]
|
||||
|
||||
//
|
||||
// Classes
|
||||
// MARK: Classes
|
||||
//
|
||||
|
||||
class Shape {
|
||||
func getArea() -> Int {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// All methods and properties of a class are public.
|
||||
// If you just need to store data in a
|
||||
// structured object, you should use a `struct`
|
||||
@ -170,6 +201,15 @@ class Rect: Shape {
|
||||
}
|
||||
}
|
||||
|
||||
// If you don't need a custom getter and setter,
|
||||
// but still want to run code before and after getting or setting
|
||||
// a property, you can use `willSet` and `didSet`
|
||||
var identifier: String = "defaultID" {
|
||||
willSet(someIdentifier) {
|
||||
print(someIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
init(sideLength: Int) {
|
||||
super.init()
|
||||
self.sideLength = sideLength
|
||||
@ -185,18 +225,21 @@ class Rect: Shape {
|
||||
return sideLength * sideLength
|
||||
}
|
||||
}
|
||||
var mySquare = new Square(sideLength: 5)
|
||||
|
||||
class Square: Rect {
|
||||
convenience init() {
|
||||
self.init(sideLength: 5)
|
||||
}
|
||||
}
|
||||
|
||||
var mySquare = Square()
|
||||
print(mySquare.getArea()) // 25
|
||||
mySquare.shrink()
|
||||
print(mySquare.sideLength) // 4
|
||||
|
||||
// If you don't need a custom getter and setter,
|
||||
// but still want to run code before and after getting or setting
|
||||
// a property, you can use `willSet` and `didSet`
|
||||
|
||||
|
||||
//
|
||||
// Enums
|
||||
// MARK: Enums
|
||||
//
|
||||
|
||||
// Enums can optionally be of a specific type or on their own.
|
||||
@ -216,12 +259,33 @@ enum Suit {
|
||||
|
||||
|
||||
//
|
||||
// Other
|
||||
// MARK: Other
|
||||
//
|
||||
|
||||
// `protocol`: Similar to Java interfaces.
|
||||
// `extension`s: Add extra functionality to an already created type
|
||||
protocol ShapeGenerator {
|
||||
func buildShape() -> Shape
|
||||
}
|
||||
|
||||
// `extension`s: Add extra functionality to an already existing type
|
||||
extension Square: Printable {
|
||||
var description: String {
|
||||
return "Area: \(self.getArea()) - ID: \(self.identifier)"
|
||||
}
|
||||
}
|
||||
|
||||
println("Square: \(mySquare)")
|
||||
|
||||
// Generics: Similar to Java. Use the `where` keyword to specify the
|
||||
// requirements of the generics.
|
||||
|
||||
func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
|
||||
for (index, value) in enumerate(array) {
|
||||
if value == valueToFind {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user