Skip to content

Commit b16c01a

Browse files
committed
Readme: added class inheritance
1 parent cbf862a commit b16c01a

File tree

1 file changed

+77
-20
lines changed

1 file changed

+77
-20
lines changed

README.md

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,34 @@
1111

1212
KeyValueCoding protocol provides a mechanism by which you can access the properties of pure Swift struct or class instances indirectly by name or key.
1313

14-
- [Overview](#Overview)
14+
- [Getting Started](#gettingstarted)
15+
- [Basics](#basics)
16+
- [Subscript](#subscript)
17+
- [Class Inheritance](#class-inheritance)
18+
- [NSObject](#nsobject)
19+
- [Struct](#struct)
20+
- [Functions](#functions)
1521
- [KeyValueCoding Protocol](#keyvaluecoding-protocol)
1622
- [metadataKind](#metadatakind)
1723
- [properties](#properties)
1824
- [value(key:)](#valuekey)
1925
- [setValue(_:, key:)](#setvalue_-key)
2026
- [[key]](#key)
21-
- [Functions](#functions)
22-
- [swift_metadataKind(of:)](swift_metadatakindof)
23-
- [swift_properties(of:)](swift_propertiesof)
24-
- [swift_value(of:, key:)](swift_valueof-key)
25-
- [swift_setValue<T>(_:, to:, key:)](swift_setvalue_-to-key)
27+
- [API](#api)
28+
- [swift_metadataKind(of:)](#swift_metadatakindof)
29+
- [swift_properties(of:)](#swift_propertiesof)
30+
- [swift_value(of:, key:)](#swift_valueof-key)
31+
- [swift_setValue<T>(_:, to:, key:)](#swift_setvalue_-to-key)
2632
- [Installation](#installation)
2733
- [License](#license)
2834

29-
## Overview
35+
## Getting Started
3036

31-
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
3238

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:
3442

3543
```swift
3644
enum UserType {
@@ -65,6 +73,8 @@ else {
6573
print(id, type, name, ssn) // 123 guest Bob 123456789
6674
```
6775

76+
### Subscript
77+
6878
You can also use subscripts to set and retrieve values by key without needing separate methods for setting and retrieval:
6979

7080
```swift
@@ -86,6 +96,29 @@ else {
8696
print(id, type, name, ssn) // 123 guest Bob 123456789
8797
```
8898

99+
### Class Inheritance
100+
101+
Properties from inherited classes are also accessible by `KeyValueCoding` protocol:
102+
103+
```swift
104+
class A: KeyValueCoding {
105+
let a = 0
106+
}
107+
108+
class B: 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+
89122
`KeyValueCoding` doesn't conflict with key-value conding of `NSObject` class and they can work together:
90123

91124
``` swift
@@ -105,7 +138,9 @@ resolution.setValue(760, key: "height")
105138
print(resolution.width, resolution.height) // 1024 760
106139
```
107140

108-
The same works with structs as well:
141+
### Struct
142+
143+
`KeyValueCoding` works with structs as well:
109144

110145
```swift
111146
struct Book: KeyValueCoding {
@@ -121,15 +156,17 @@ book["ISBN"] = 1234567890
121156
print(book) // Book(title: "The Swift Programming Language", ISBN: 1234567890)
122157
```
123158

124-
In additional there are also global functions to set and get values of properties without adopting `KeyValueCoding` protocol:
159+
### Functions
160+
161+
In additional there are also API functions to set and get values of properties without adopting `KeyValueCoding` protocol:
125162

126163
``` swift
127164
struct Song {
128-
let name: String = ""
129-
let artist: String = ""
165+
let name: String
166+
let artist: String
130167
}
131168

132-
var song = Song()
169+
var song = Song(name: "", artist: "")
133170

134171
swift_setValue("Blue Suede Shoes", to: &song, key: "name")
135172
swift_setValue("Elvis Presley", to: &song, key: "artist")
@@ -218,9 +255,9 @@ if let type = user["type"] as? UserType {
218255
}
219256
```
220257

221-
## Functions
258+
## API
222259

223-
Global functions to set, get and retrieve metadata information from any instance or type without adopting `KeyValueCoding` protocol.
260+
Global functions to set, get and retrieve metadata information from any instance or type (even without adopting `KeyValueCoding` protocol).
224261

225262
### swift_metadataKind(of:)
226263

@@ -263,8 +300,32 @@ PropertyMetadata(name: "artist", type: Swift.String, isStrong: true, isVar: fals
263300

264301
### swift_value(of:, key:)
265302

303+
Returns the value for the instance's property identified by a given key.
304+
305+
```swift
306+
var song = Song(name: "Blue Suede Shoes", artist: "Elvis Presley")
307+
308+
guard let name = swift_value(of: &song, key: "name"),
309+
let aritst = swift_value(of: &song, key: "artist")
310+
else {
311+
return
312+
}
313+
314+
print(name, "-", aritst) // Blue Suede Shoes - Elvis Presley
315+
```
316+
266317
### swift_setValue(_:, to:, key:)
267318

319+
Sets a property of an instance specified by a given key to a given value.
320+
321+
```swift
322+
var song = Song(name: "", artist: "")
323+
324+
swift_setValue("Blue Suede Shoes", to: &song, key: "name")
325+
swift_setValue("Elvis Presley", to: &song, key: "artist")
326+
327+
print(song.name, "-", song.artist) // Blue Suede Shoes - Elvis Presley
328+
```
268329

269330
## Installation
270331

@@ -299,8 +360,4 @@ let package = Package(
299360

300361
KeyValueCoding is available under the MIT license. See the [LICENSE](LICENSE) file for more info.
301362

302-
<p align="center">
303-
304363
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/donate/?hosted_button_id=TSPDD3ZAAH24C)
305-
306-
</p>

0 commit comments

Comments
 (0)