Skip to content

Commit

Permalink
Updated syntax for 3.7.3 (#7)
Browse files Browse the repository at this point in the history
* Updated syntax for 3.7.3

The original example won't compile because computed properties can't have willSet/didSet. Also setting a computed property in its own setter throws a warning. This new code will compile (except for the someBool part)

* Update README.md
  • Loading branch information
Dan Deng authored and cezarywojcik committed Sep 28, 2016
1 parent 4bd7886 commit b748bc8
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Make sure to read [Apple's API Design Guidelines](https://swift.org/documentatio

Specifics from these guidelines + additional remarks are mentioned below.

This guide was last updated for Swift 2.2 on August 31st, 2016.
This guide was last updated for Swift 2.2 on September 27th, 2016.

## Table Of Contents

Expand Down Expand Up @@ -631,21 +631,24 @@ var computedProperty: String {
* **3.7.3** Though you can create a custom name for the new or old value for `willSet`/`didSet` and `set`, use the standard `newValue`/`oldValue` identifiers that are provided by default.

```swift
var computedProperty: String {
var storedProperty: String = "I'm selling these fine leather jackets." {
willSet {
print("will set to \(newValue)")
}
didSet {
print("did set from \(oldValue) to \(storedProperty)")
}
}

var computedProperty: String {
get {
if someBool {
return "I'm a mighty pirate!"
}
return "I'm selling these fine leather jackets."
return storedProperty
}
set {
computedProperty = newValue
}
willSet {
print("will set to \(newValue)")
}
didSet {
print("did set from \(oldValue) to \(newValue)")
storedProperty = newValue
}
}
```
Expand Down

0 comments on commit b748bc8

Please sign in to comment.