https://github.com/loro-dev/loro-swift
Swift bindings of Loro CRDTs
https://github.com/loro-dev/loro-swift
crdts loro loro-crdt swift
Last synced: 4 months ago
JSON representation
Swift bindings of Loro CRDTs
- Host: GitHub
- URL: https://github.com/loro-dev/loro-swift
- Owner: loro-dev
- License: mit
- Created: 2024-08-06T03:29:19.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-12-09T10:59:15.000Z (6 months ago)
- Last Synced: 2026-02-10T19:18:49.262Z (4 months ago)
- Topics: crdts, loro, loro-crdt, swift
- Language: Swift
- Homepage: https://loro.dev
- Size: 21.9 MB
- Stars: 35
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://swiftpackageindex.com/loro-dev/loro-swift)
[](https://swiftpackageindex.com/loro-dev/loro-swift)
loro-swift
This repository contains experimental Swift bindings for
[Loro CRDT](https://github.com/loro-dev/loro).
If you have any suggestions for API, please feel free to create an issue or join
our [Discord](https://discord.gg/tUsBSVfqzf) community.
## Usage
Add the dependency in your `Package.swift`.
```swift
let package = Package(
name: "your-project",
products: [......],
dependencies:[
...,
.package(url: "https://github.com/loro-dev/loro-swift.git", from: "1.10.3")
],
targets:[
.executableTarget(
...,
dependencies:[.product(name: "Loro", package: "loro-swift")],
)
]
)
```
## Examples
```swift
import Loro
// create a Loro document
let doc = LoroDoc()
// create Root Container by getText, getList, getMap, getTree, getMovableList, getCounter
let text = doc.getText(id: "text")
try! text.insert(pos: 0, s: "abc")
try! text.delete(pos: 0, len: 1)
let s = text.toString()
// XCTAssertEqual(s, "bc")
// subscribe the event
let sub = doc.subscribeRoot{ diffEvent in
print(diffEvent)
}
// export updates or snapshot
let doc2 = LoroDoc()
let snapshot = doc.export(mode: ExportMode.snapshot)
let updates = doc.export(mode: ExportMode.updates(from: VersionVector()))
// import updates or snapshot
let status = try! doc2.import(snapshot)
let status2 = try! doc2.import(updates)
// import batch of updates or snapshot
try! doc2.importBatch(bytes: [snapshot, updates])
// checkout to any version
let startFrontiers = doc.oplogFrontiers()
try! doc.checkout(frontiers: startFrontiers)
doc.checkoutToLatest()
```
## Develop
If you wanna build and develop this project with MacOS, you need first run this
script:
```bash
sh ./scripts/build_macos.sh
LOCAL_BUILD=1 swift test
```
The script will run `uniffi` and generate the `loroFFI.xcframework.zip`.
# Credits
- [uniffi-rs](https://github.com/mozilla/uniffi-rs): a multi-language bindings generator for rust
- [Automerge-swift](https://github.com/automerge/automerge-swift): `loro-swift`
uses many of `automerge-swift`'s scripts for building and CI.