|
| 1 | +#Exporting Swift Classes to JS |
| 2 | + |
| 3 | +Learn how to export Swift classes to JavaScript. |
| 4 | + |
| 5 | +##Overview |
| 6 | + |
| 7 | +>Tip: You can quickly preview what interfaces will be exposed on the Swift/TypeScript sides using the[BridgeJS Playground](https://swiftwasm.org/JavaScriptKit/PlayBridgeJS/). |
| 8 | +
|
| 9 | +To export a Swift class, mark both the class and any members you want to expose: |
| 10 | + |
| 11 | +```swift |
| 12 | +importJavaScriptKit |
| 13 | + |
| 14 | +@JSclassShoppingCart { |
| 15 | +privatevar items: [(name:String, price:Double, quantity:Int)]= [] |
| 16 | + |
| 17 | +@JSinit() {} |
| 18 | + |
| 19 | +@JSpublicfuncaddItem(name:String,price:Double,quantity:Int) { |
| 20 | + items.append((name, price, quantity)) |
| 21 | + } |
| 22 | + |
| 23 | +@JSpublicfuncremoveItem(atIndexindex:Int) { |
| 24 | +guard index>=0&& index< items.countelse {return } |
| 25 | + items.remove(at: index) |
| 26 | + } |
| 27 | + |
| 28 | +@JSpublicfuncgetTotal()->Double { |
| 29 | +return items.reduce(0) {$0+$1.price*Double($1.quantity) } |
| 30 | + } |
| 31 | + |
| 32 | +@JSpublicfuncgetItemCount()->Int { |
| 33 | +return items.count |
| 34 | + } |
| 35 | + |
| 36 | +// This method won't be accessible from JavaScript (no @JS) |
| 37 | +var debugDescription:String { |
| 38 | +return"Cart with\(items.count) items, total:\(getTotal())" |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +In #"diff-ce40517437361e241038e3e284023203f10556ac6a19f3d7da91c584f136a6bd-empty-44-0" data-selected="false" role="gridcell" tabindex="-1" valign="top"> | 44 | + |
| 45 | +```javascript |
| 46 | +import {init }from"./.build/plugins/PackageToJS/outputs/Package/index.js"; |
| 47 | +const {exports }=awaitinit({}); |
| 48 | + |
| 49 | +constcart=newexports.ShoppingCart(); |
| 50 | +cart.addItem("Laptop",999.99,1); |
| 51 | +cart.addItem("Mouse",24.99,2); |
| 52 | +console.log(`Items in cart:${cart.getItemCount()}`); |
| 53 | +console.log(`Total: $${cart.getTotal().toFixed(2)}`); |
| 54 | +``` |
| 55 | + |
| 56 | +The generated TypeScript declarations for this class would look like: |
| 57 | + |
| 58 | +```typescript |
| 59 | +// Base interface for Swift reference types |
| 60 | +exportinterfaceSwiftHeapObject { |
| 61 | + release():void; |
| 62 | +} |
| 63 | + |
| 64 | +// ShoppingCart interface with all exported methods |
| 65 | +exportinterfaceShoppingCartextendsSwiftHeapObject { |
| 66 | + addItem(name:string,price:number,quantity:number):void; |
| 67 | + removeItem(atIndex:number):void; |
| 68 | + getTotal():number; |
| 69 | + getItemCount():number; |
| 70 | +} |
| 71 | + |
| 72 | +exporttypeExports= { |
| 73 | + ShoppingCart: { |
| 74 | +new():ShoppingCart; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | +
|
| 79 | +## Supported Features |
| 80 | +
|
| 81 | +| Swift Feature | Status | |
| 82 | +|:--------------|:-------| |
| 83 | +| Initializers:`init()` | ✅ | |
| 84 | +| Initializers that throw JSException:`init()throws(JSException)` | ✅ | |
| 85 | +| Initializers that throw any exception:`init()throws` | ❌ | |
| 86 | +| Async initializers:`init()async` | ❌ | |
| 87 | +| Deinitializers:`deinit` | ✅ | |
| 88 | +| Stored properties:`var`,`let` (with`willSet`,`didSet`) | ✅ | |
| 89 | +| Computed properties:`varx:X {get set }` | ✅ | |
| 90 | +| Computed properties with effects:`varx:X {getasync throws }` | 🚧 | |
| 91 | +| Methods:`func` | ✅ (See <doc:Exporting-Swift-Function> ) | |
| 92 | +| Static/class methods:`staticfunc`,`classfunc` | 🚧 | |
| 93 | +| Subscripts:`subscript()` | ❌ | |
| 94 | +| Generics | ❌ | |