You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The basic methods of `KeyValueCoding` protocol for accessing an instance’s values are `setValue(_ value: Any?, key: String)`, which sets the value for the property identified by the specified key, and `value(key: String) -> Any?`, which returns the value for the property identified by the specified key. Thus, all of an instance’s properties including properties with `enum` and `Optional` types can be accessed in a consistent manner.
37
+
## Basics
32
38
33
-
In order to make your own instances key-value coding compliant just adopt them from the `KeyValueCoding` protocol:
39
+
The basic methods of `KeyValueCoding` protocol for accessing an instance’s values are `setValue(_ value: Any?, key: String)`, which sets the value for the property identified by the specified key, and `value(key: String) -> Any?`, which returns the value for the property identified by the specified key. Thus, all of an instance’s properties (including properties with `enum` ,`Optional` and etc. types) can be accessed in a consistent manner.
40
+
41
+
In order to make your types key-value coding compliant just adopt them from the `KeyValueCoding` protocol:
34
42
35
43
```swift
36
44
enumUserType {
@@ -65,6 +73,8 @@ else {
65
73
print(id, type, name, ssn) // 123 guest Bob 123456789
66
74
```
67
75
76
+
### Subscript
77
+
68
78
You can also use subscripts to set and retrieve values by key without needing separate methods for setting and retrieval:
69
79
70
80
```swift
@@ -86,6 +96,29 @@ else {
86
96
print(id, type, name, ssn) // 123 guest Bob 123456789
87
97
```
88
98
99
+
### Class Inheritance
100
+
101
+
Properties from inherited classes are also accessible by `KeyValueCoding` protocol:
102
+
103
+
```swift
104
+
classA: KeyValueCoding {
105
+
let a =0
106
+
}
107
+
108
+
classB: A {
109
+
let b =0
110
+
}
111
+
112
+
var b =B()
113
+
114
+
b["a"] =1
115
+
b["b"] =2
116
+
117
+
print(b["a"]!, b["b"]!) // 1 2
118
+
```
119
+
120
+
### NSObject
121
+
89
122
`KeyValueCoding` doesn't conflict with key-value conding of `NSObject` class and they can work together:
0 commit comments