Skip to content

Commit cbcfa2c

Browse files
committed
Update example code in README and documentation
1 parent 2877932 commit cbcfa2c

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,22 @@ do {
6161
### Multicodec
6262

6363
```swift
64+
// Original binary data
65+
let rawData = Data([0xde, 0xad, 0xbe, 0xef])
66+
67+
// Select a codec (e.g., raw binary)
68+
let codec = Multicodec.raw
69+
70+
// Wrap the raw data with the codec’s prefix
71+
let wrapped = codec.wrap(rawData)
72+
print("Wrapped: \(wrapped)")
73+
74+
// Unwrap the data
6475
do {
65-
let dagPB = try Multicodec(name: "dag-pb", tag: "dag-pb", code: 0x70, status: .permanent)
66-
try await MulticodecRegistry.shared.register(dagPB)
67-
68-
let dagPBEntry = MulticodecRegistry.shared.get(name: "dag-pb")
69-
let data = Data("Hello from Multicodec!".utf8)
70-
let wrapped = try await Multicodec.shared.wrap(dagPBEntry, rawData: data)
71-
72-
let (unwrappedCodec, unwrappedData) = try await MulticodecRegistry.shared.unwrap(wrapped)
73-
print(unwrappedCodec.name) // "dag-pb"
74-
75-
if let string = String(data: unwrappedData, encoding: .utf8) {
76-
print(string) // Will return as "dag-pb".
77-
} else {
78-
print("Failed to decode UTF-8 string")
79-
}
80-
76+
let unwrapped = try codec.unwrap(wrapped)
77+
print("Unwrapped: \(unwrapped)")
8178
} catch {
82-
throw error
79+
print(error)
8380
}
8481
```
8582

Sources/MultiformatsKit/Multicodec/Multicodec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import Foundation
3737
/// // Unwrap the data
3838
/// do {
3939
/// let unwrapped = try codec.unwrap(wrapped)
40-
/// print("Unwrapped: \(unwrapped)") // <deadbeef>
40+
/// print("Unwrapped: \(unwrapped)")
4141
/// } catch {
4242
/// print(error)
4343
/// }

0 commit comments

Comments
 (0)