mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Added clearer description of Optionals and Unwrapping. Minor typo changes as well.
This commit is contained in:
parent
5dac348b72
commit
ad16a31c07
@ -58,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.
|
||||||
@ -75,11 +76,17 @@ if someOptionalString != nil {
|
|||||||
if someOptionalString!.hasPrefix("opt") {
|
if someOptionalString!.hasPrefix("opt") {
|
||||||
print("has the prefix")
|
print("has the prefix")
|
||||||
}
|
}
|
||||||
|
|
||||||
let empty = someOptionalString?.isEmpty
|
let empty = someOptionalString?.isEmpty
|
||||||
}
|
}
|
||||||
someOptionalString = nil
|
someOptionalString = nil
|
||||||
|
|
||||||
|
/*
|
||||||
|
To get the underlying type from an optional, you unwrap it using the
|
||||||
|
force unwrap operator (!). Only use the unwrap operator if you're sure
|
||||||
|
the underlying value isn't nil.
|
||||||
|
*/
|
||||||
|
|
||||||
// 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)
|
||||||
@ -94,13 +101,13 @@ 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."
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Comment here
|
Comment here
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Nested comments are also supported
|
Nested comments are also supported
|
||||||
*/
|
*/
|
||||||
@ -296,10 +303,10 @@ 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]()
|
||||||
|
|
||||||
// Custom subscript
|
// Custom subscript
|
||||||
subscript(index: Int) -> String {
|
subscript(index: Int) -> String {
|
||||||
return names[index]
|
return names[index]
|
||||||
@ -330,7 +337,7 @@ public class Shape {
|
|||||||
|
|
||||||
internal class Rect: Shape {
|
internal class Rect: Shape {
|
||||||
var sideLength: Int = 1
|
var sideLength: Int = 1
|
||||||
|
|
||||||
// Custom getter and setter property
|
// Custom getter and setter property
|
||||||
private var perimeter: Int {
|
private var perimeter: Int {
|
||||||
get {
|
get {
|
||||||
@ -341,11 +348,11 @@ internal class Rect: Shape {
|
|||||||
sideLength = newValue / 4
|
sideLength = newValue / 4
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lazily load a property
|
// Lazily load a property
|
||||||
// subShape remains nil (uninitialized) until getter called
|
// subShape remains nil (uninitialized) until getter called
|
||||||
lazy var subShape = Rect(sideLength: 4)
|
lazy var subShape = Rect(sideLength: 4)
|
||||||
|
|
||||||
// If you don't need a custom getter and setter,
|
// If you don't need a custom getter and setter,
|
||||||
// but still want to run code before and after getting or setting
|
// but still want to run code before and after getting or setting
|
||||||
// a property, you can use `willSet` and `didSet`
|
// a property, you can use `willSet` and `didSet`
|
||||||
@ -355,19 +362,19 @@ internal class Rect: Shape {
|
|||||||
print(someIdentifier)
|
print(someIdentifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init(sideLength: Int) {
|
init(sideLength: Int) {
|
||||||
self.sideLength = sideLength
|
self.sideLength = sideLength
|
||||||
// always super.init last when init custom properties
|
// always super.init last when init custom properties
|
||||||
super.init()
|
super.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
func shrink() {
|
func shrink() {
|
||||||
if sideLength > 0 {
|
if sideLength > 0 {
|
||||||
--sideLength
|
--sideLength
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func getArea() -> Int {
|
override func getArea() -> Int {
|
||||||
return sideLength * sideLength
|
return sideLength * sideLength
|
||||||
}
|
}
|
||||||
@ -399,13 +406,13 @@ class Circle: Shape {
|
|||||||
override func getArea() -> Int {
|
override func getArea() -> Int {
|
||||||
return 3 * radius * radius
|
return 3 * radius * radius
|
||||||
}
|
}
|
||||||
|
|
||||||
// Place a question mark postfix after `init` is an optional init
|
// Place a question mark postfix after `init` is an optional init
|
||||||
// which can return nil
|
// which can return nil
|
||||||
init?(radius: Int) {
|
init?(radius: Int) {
|
||||||
self.radius = radius
|
self.radius = radius
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
if radius <= 0 {
|
if radius <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -459,7 +466,7 @@ enum Furniture {
|
|||||||
case Desk(height: Int)
|
case Desk(height: Int)
|
||||||
// Associate with String and Int
|
// Associate with String and Int
|
||||||
case Chair(String, Int)
|
case Chair(String, Int)
|
||||||
|
|
||||||
func description() -> String {
|
func description() -> String {
|
||||||
switch self {
|
switch self {
|
||||||
case .Desk(let height):
|
case .Desk(let height):
|
||||||
@ -498,7 +505,7 @@ protocol ShapeGenerator {
|
|||||||
|
|
||||||
class MyShape: Rect {
|
class MyShape: Rect {
|
||||||
var delegate: TransformShape?
|
var delegate: TransformShape?
|
||||||
|
|
||||||
func grow() {
|
func grow() {
|
||||||
sideLength += 2
|
sideLength += 2
|
||||||
|
|
||||||
@ -533,7 +540,7 @@ extension Int {
|
|||||||
var customProperty: String {
|
var customProperty: String {
|
||||||
return "This is \(self)"
|
return "This is \(self)"
|
||||||
}
|
}
|
||||||
|
|
||||||
func multiplyBy(num: Int) -> Int {
|
func multiplyBy(num: Int) -> Int {
|
||||||
return num * self
|
return num * self
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user