summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLevi Bostian <levi.bostian@gmail.com>2015-10-09 10:08:24 -0500
committerLevi Bostian <levi.bostian@gmail.com>2015-10-09 10:08:24 -0500
commit10ac3c9eb73f3def5de76d1383f86d4423e0b393 (patch)
tree570d979b7501b58c11b7f36d481fb15eb7f7a837
parenteafe2a871528acc1195e07ef4670d1e8df8f5531 (diff)
parentb4860de42f2bbf0ab97ef28085eb40accb030657 (diff)
Merge pull request #1381 from cwalk/swift-2
[swift/en] Added clearer description of Optionals and Unwrapping.
-rw-r--r--swift.html.markdown16
1 files changed, 12 insertions, 4 deletions
diff --git a/swift.html.markdown b/swift.html.markdown
index 75535e43..f451288d 100644
--- a/swift.html.markdown
+++ b/swift.html.markdown
@@ -5,6 +5,7 @@ contributors:
- ["Christopher Bess", "http://github.com/cbess"]
- ["Joey Huang", "http://github.com/kamidox"]
- ["Anthony Nguyen", "http://github.com/anthonyn60"]
+ - ["Clayton Walker", "https://github.com/cwalk"]
filename: learnswift.swift
---
@@ -57,8 +58,9 @@ let piText = "Pi = \(π), Pi 2 = \(π * 2)" // String interpolation
print("Build value: \(buildValue)") // Build value: 7
/*
- Optionals are a Swift language feature that allows you to store a `Some` or
- `None` value.
+ Optionals are a Swift language feature that either contains a 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
explicitly stored as an Optional value.
@@ -79,6 +81,12 @@ if 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
var unwrappedString: String! = "Value is expected."
// 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.
// 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
anyObjectVar = "Changed value to a string, not good practice, but possible."
@@ -295,7 +303,7 @@ print(numbers) // [3, 6, 18]
// MARK: Structures
//
-// Structures and classes have very similar capabilites
+// Structures and classes have very similar capabilities
struct NamesTable {
let names = [String]()