-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38569d2
commit a6ca0c8
Showing
24 changed files
with
2,691 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
0230-composable-navigation-pt9/Inventory/COW.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
struct Wrapper<Value> { | ||
var value: Value { | ||
get { self.storage.value } | ||
set { | ||
if Swift.isKnownUniquelyReferenced(&self.storage) { | ||
self.storage.value = newValue | ||
} else { | ||
self.storage = Storage(value: newValue) | ||
} | ||
} | ||
} | ||
|
||
private var storage: Storage | ||
|
||
init(value: Value) { | ||
self.storage = Storage(value: value) | ||
} | ||
|
||
mutating func isKnownUniquelyReferenced() -> Bool { | ||
Swift.isKnownUniquelyReferenced(&self.storage) | ||
} | ||
|
||
private class Storage { | ||
var value: Value | ||
init(value: Value) { | ||
self.value = value | ||
} | ||
} | ||
} | ||
|
||
import Foundation | ||
|
||
extension Wrapper: Equatable where Value: Equatable { | ||
static func == (lhs: Self, rhs: Self) -> Bool { | ||
if lhs.storage === rhs.storage { | ||
return true | ||
} | ||
Thread.sleep(forTimeInterval: 3) | ||
return lhs.value == rhs.value | ||
} | ||
} | ||
|
||
var x = Wrapper(value: 1) | ||
x.isKnownUniquelyReferenced() | ||
var y = x | ||
x.isKnownUniquelyReferenced() | ||
y.isKnownUniquelyReferenced() | ||
x == y | ||
x.value = 2 | ||
y.value = 2 | ||
x == y | ||
y.value | ||
x.value | ||
x.isKnownUniquelyReferenced() | ||
y.isKnownUniquelyReferenced() | ||
|
||
var z = 1 | ||
var w = z | ||
z = 2 | ||
w | ||
z |
4 changes: 4 additions & 0 deletions
4
0230-composable-navigation-pt9/Inventory/COW.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
Oops, something went wrong.