title: Swift Setup sidebar_position: 8 id: swift license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Fory Swift provides xlang Object Serialization and compiler-generated models. It is distributed through Swift Package Manager, uses Swift tools 6.0, and targets macOS 13 or later and iOS 16 or later.
swift --version
Create an executable package:
swift package init --type executable --name ForyExample
Add the released package and depend on its Fory library in the generated Package.swift:
dependencies: [ .package(url: "https://github.com/apache/fory.git", exact: "1.6.1") ], targets: [ .executableTarget( name: "ForyExample", dependencies: [.product(name: "Fory", package: "fory")] ) ]
Replace Sources/main.swift with:
import Fory @ForyStruct struct User: Equatable { var id: Int64 = 0 var name: String = "" } let fory = Fory() try fory.register(User.self, id: 1) let input = User(id: 1, name: "Alice") let bytes = try fory.serialize(input) let decoded: User = try fory.deserialize(bytes) assert(input == decoded)
swift run
Swift uses xlang mode. Continue with Swift Object Serialization, xlang types, configuration, and schema evolution.