To get started with SwiftyJS:
- Create a protocol ποΈ
- Use
@SwiftyJS
macro βοΈ - Set JavaScript code or file to generated class. β¨οΈ
- And done π
@SwiftyJS
protocol DataCreator {
var lastUser: User { get throws }
func createUser() throws -> User
}
let js = DataCreatorJSBridge()
js.loadFrom(jsCode: """
var lastUser = null;
function createUser() {
let user = {
id: 10,
name: "Yusuf",
score:5.1,
}
lastUser = user;
return user;
}
""")
print(try js.createUser())
// Output: User(id: "10", name: "Yusuf", score: 5.1)
try js.setLastUser(User(id: "7", name: "John", score: 1.3))
print(try js.lastUser)
// Output: User(id: "7", name: "John", score: 1.3)
- π₯Seamless Swift-JavaScript Interaction: Integrate SwiftyJS into your Swift projects effortlessly. Define a Protocol with the @SwiftyJS macro and interact with JavaScript code seamlessly.
- πLoading JavaScript Code: Load JavaScript code files or string using the
{ProtocolName}JSBridge
class. - π€Call JavaScript Functions: Using the Generated Class, call JavaScript functions and get results in a Swift-like manner.
- π§ΎAccessing JavaScript Data: Seamlessly access JavaScript data from Swift using swift variable.
- πUpdating JavaScript Data: SwiftyJS also allows you to update JavaScript data from Swift. You can set the variable with setter function.
For Xcode project
You can add SwiftyJS macro to your project as a package.
https://github.com/yusufozgul/SwiftyJS
For Swift Package Manager
In Package.swift
add:
dependencies: [
.package(url: "https://github.com/yusufozgul/SwiftyJS", from: "0.0.1"),
]
and then add the product to any target that needs access to the macro:
.product(name: "SwiftyJS", package: "SwiftyJS"),
This library is released under the MIT license. See LICENSE for details.