1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-01-17 13:38:38 +01:00

Merge pull request #1381 from cwalk/swift-2

[swift/en] Added clearer description of Optionals and Unwrapping.
This commit is contained in:
Levi Bostian 2015-10-09 10:08:24 -05:00
commit 10ac3c9eb7

View File

@ -5,6 +5,7 @@ contributors:
- ["Christopher Bess", "http://github.com/cbess"] - ["Christopher Bess", "http://github.com/cbess"]
- ["Joey Huang", "http://github.com/kamidox"] - ["Joey Huang", "http://github.com/kamidox"]
- ["Anthony Nguyen", "http://github.com/anthonyn60"] - ["Anthony Nguyen", "http://github.com/anthonyn60"]
- ["Clayton Walker", "https://github.com/cwalk"]
filename: learnswift.swift filename: learnswift.swift
--- ---
@ -57,8 +58,9 @@ let piText = "Pi = \(π), Pi 2 = \(π * 2)" // String interpolation
print("Build value: \(buildValue)") // Build value: 7 print("Build value: \(buildValue)") // Build value: 7
/* /*
Optionals are a Swift language feature that allows you to store a `Some` or Optionals are a Swift language feature that either contains a value,
`None` value. or contains nil (no value) to indicate that a value is missing.
A question mark (?) after the type marks the value as optional.
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. explicitly stored as an Optional value.
@ -79,6 +81,12 @@ if someOptionalString != nil {
} }
someOptionalString = nil someOptionalString = nil
/*
Trying to use ! to access a non-existent optional value triggers a runtime
error. Always make sure that an optional contains a non-nil value before
using ! to force-unwrap its value.
*/
// implicitly unwrapped optional // implicitly unwrapped optional
var unwrappedString: String! = "Value is expected." var unwrappedString: String! = "Value is expected."
// same as above, but ! is a postfix operator (more syntax candy) // same as above, but ! is a postfix operator (more syntax candy)
@ -93,7 +101,7 @@ if let someOptionalStringConstant = someOptionalString {
// Swift has support for storing a value of any type. // Swift has support for storing a value of any type.
// AnyObject == id // AnyObject == id
// Unlike Objective-C `id`, AnyObject works with any value (Class, Int, struct, etc) // Unlike Objective-C `id`, AnyObject works with any value (Class, Int, struct, etc.)
var anyObjectVar: AnyObject = 7 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."
@ -295,7 +303,7 @@ print(numbers) // [3, 6, 18]
// MARK: Structures // MARK: Structures
// //
// Structures and classes have very similar capabilites // Structures and classes have very similar capabilities
struct NamesTable { struct NamesTable {
let names = [String]() let names = [String]()