此仓库包含用于 Loro CRDT 的实验性 Swift 绑定。
如果您对 API 有任何建议,请随时创建 issue 或加入我们的 Discord 社区。
在您的 Package.swift
文件中添加依赖项。
let package = Package(
name: "your-project",
products: [......],
dependencies:[
...,
.package(url: "https://github.com/loro-dev/loro-swift.git", from: "1.4.1")
],
targets:[
.executableTarget(
...,
dependencies:[.product(name: "Loro", package: "loro-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()
如果您想在 MacOS 上构建和开发此项目,您需要首先运行此脚本
sh ./scripts/build_macos.sh
LOCAL_BUILD=1 swift test
该脚本将运行 uniffi
并生成 loroFFI.xcframework.zip
。
loro-swift
使用了 automerge-swift
的许多脚本用于构建和 CI。