Formatted the document according to the guidelines

This commit is contained in:
Grant Timmerman 2014-06-04 13:22:01 -07:00
parent 0d984535bd
commit 76f4c1b3b6

View File

@ -9,20 +9,11 @@ Swift is a programming language for iOS and OS X development created by Apple. D
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.
# Overview
- [Basics](#basics)
- [Arrays and Dictionaries](#array)
- [Control Flow](#control)
- [Functions](#func)
- [Closures](#closures)
- [Classes](#classes)
- [Enums](#enums)
- [Other](#other)
- [Links](#links)
## <a name="basics"></a>Basics
```js
//
// Basics
//
println("Hello, world")
var myVariable = 42
let myConstant = 3.1415926
@ -31,11 +22,12 @@ let label = "some text " + String(myVariable) // Casting
let piText = "Pi = \(myConstant)" // String interpolation
var optionalString: String? = "optional" // Can be nil
optionalString = nil
```
## <a name="array"></a>Arrays and Dictionaries
```js
//
// Arrays and Dictionaries
//
// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
@ -48,11 +40,12 @@ var occupations = [
]
occupations["Jayne"] = "Public Relations"
let emptyDictionary = Dictionary<String, Float>()
```
## <a name="control"></a>Control Flow
```js
//
// Control Flow
//
// for loop (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
@ -97,13 +90,15 @@ case let x where x.hasSuffix("pepper"):
default: // required (in order to cover all possible input)
let vegetableComment = "Everything tastes good in soup."
}
```
## <a name="functions"></a>Functions
Functions are a first-class type, meaning they can be nested in functions and can be passed around
//
// Functions
//
// Functions are a first-class type, meaning they can be nested
// in functions and can be passed around
```js
// Function
func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
@ -127,13 +122,14 @@ func makeIncrementer() -> (Int -> Int) {
}
var increment = makeIncrementer()
increment(7)
```
## <a name="closures"></a>Closures
Functions are special case closures ({})
//
// Closures
//
// Functions are special case closures ({})
```js
// Closure example.
// `->` separates the arguments and return type
// `in` separates the closure header from the closure body
@ -147,14 +143,16 @@ numbers.map({
var numbers = [1, 2, 6]
numbers = numbers.map({ number in 3 * number })
print(numbers) // [3, 6, 18]
```
## <a name="classes"></a>Classes
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`
//
// Classes
//
// 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`
```js
// A simple class `Square` extends `Shape
class Rect: Shape {
var sideLength: Int = 1
@ -189,15 +187,18 @@ 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 an after getting or setting a property, you can use `willSet` and `didSet`
```
// If you don't need a custom getter and setter,
// but still want to run code before an after getting or setting
// a property, you can use `willSet` and `didSet`
## <a name="enums"></a>Enums
Enums can optionally be of a specific type or on their own. They can contain methods like classes.
//
// Enums
//
// Enums can optionally be of a specific type or on their own.
// They can contain methods like classes.
```js
enum Suit {
case Spades, Hearts, Diamonds, Clubs
func getIcon() -> String {
@ -209,16 +210,15 @@ enum Suit {
}
}
}
//
// Other
//
// `protocol`: Similar to Java interfaces.
// `extension`s: Add extra functionality to an already created type
// Generics: Similar to Java. Use the `where` keyword to specify the
// requirements of the generics.
```
## <a name="other"></a>Other
- **`protocol`**: Similar to Java interfaces.
- **`extension`s**: Add extra functionality to an already created type
- **Generics**: Similar to Java. Use the `where` keyword to specify the requirements of the generics.
## <a name="links"></a>Links
- [Homepage](https://developer.apple.com/swift/)
- [Guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/LandingPage/index.html)
- [Book](https://itunes.apple.com/us/book/the-swift-programming-language/id881256329?mt=11)