Merge pull request #574 from Bijiabo/master
add @objc note for extending module using swift
diff --git a/docs/guide/extend/extend-ios-with-swift.md b/docs/guide/extend/extend-ios-with-swift.md
index 1671a40..fbae869 100644
--- a/docs/guide/extend/extend-ios-with-swift.md
+++ b/docs/guide/extend/extend-ios-with-swift.md
@@ -16,14 +16,13 @@
- implementation in `WXSwiftTestModule.h/m`
- WXSwiftTestModule.h
- ```
- #import <Foundation/Foundation.h>
- #import <WeexSDK/WeexSDK.h>
+ ```swift
+ #import <Foundation/Foundation.h>
+ #import <WeexSDK/WeexSDK.h>
- @interface WXSwiftTestModule : NSObject <WXModuleProtocol>
+ @interface WXSwiftTestModule : NSObject <WXModuleProtocol>
- @end
-
+ @end
```
- WXSwiftTestModule.m
@@ -34,41 +33,45 @@
weex/ios/playground/DerivedData/WeexDemo/Build/Intermediates/WeexDemo.build/Debug-iphonesimulator/WeexDemo.build/DerivedSources/WeexDemo-Swift.h
```
export method define in swift file.
- ```
- #import "WXSwiftTestModule.h"
- #import "WeexDemo-Swift.h" // you need to import the header to make Objective-C code recognize the method defined in swift file.
+ ```swift
+ #import "WXSwiftTestModule.h"
+ #import "WeexDemo-Swift.h" // you need to import the header to make Objective-C code recognize the method defined in swift file.
- @implementation WXSwiftTestModule
- #pragma clang diagnostic push //forbid unknow selector warrning
- #pragma clang diagnostic ignored "-Wundeclared-selector"
+ @implementation WXSwiftTestModule
+ #pragma clang diagnostic push //forbid unknow selector warrning
+ #pragma clang diagnostic ignored "-Wundeclared-selector"
- WX_EXPORT_METHOD(@selector(printSome:callback:)) //method name in Swift, you can get the final method name in `WeexDemo-Swift.h` as the convert of Xcode.
+ WX_EXPORT_METHOD(@selector(printSome:callback:)) //method name in Swift, you can get the final method name in `WeexDemo-Swift.h` as the convert of Xcode.
- #pragma clang diagnostic pop
+ #pragma clang diagnostic pop
- @end
-
+ @end
```
- in Swift
make an extension for Objective-C class `WXSwiftTestModule`, add a method, and then export it in Objective-C, then we can use it in javaScript.
- WXSwiftTestModule.swift
- ```
- import Foundation
- public extension WXSwiftTestModule {
- public func printSome(someThing:String, callback:WXModuleCallback) {
- print(someThing)
- callback(someThing)
- }
- }
+ ```swift
+ import Foundation
+ public extension WXSwiftTestModule {
+ @objc(printSome:callback:)
+ public func printSome(someThing:String, callback:WXModuleCallback) {
+ print(someThing)
+ callback(someThing)
+ }
+ }
```
- we need to expose `WXSwiftTestModule` `WXModuleCallback` in `WeexDemo-Bridging-Header` as our `Objective-C` need to access them
+ we need to expose `WXSwiftTestModule` `WXModuleCallback` in `WeexDemo-Bridging-Header` as our `Objective-C` need to access them.
+
+ Attention: Please add the `@objc` attribute to the method in the Swift file to avoid the error for the method cannot be found.
+
+
- WeexDemo-Bridging-Header.h
- ```
+ ```swift
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
@@ -81,26 +84,25 @@
### module usage
- register module to WeexSDK
- ```
+ ```Objective-C
[WXSDKEngine registerModule:@"swifter" withClass:[WXSwiftTestModule class]];
```
- front-end usage
- ```
- <template>
- <text>Swift Module</text>
- </template>
+ ```html
+ <template>
+ <text>Swift Module</text>
+ </template>
- <script>
- module.exports = {
- ready: function() {
- var swifter = weex.require('swifter');
- swifter.printSome("https://www.taobao.com",function(param){
- nativeLog(param);
- });
- }
-
- };
- </script>
+ <script>
+ module.exports = {
+ ready: function() {
+ var swifter = weex.require('swifter');
+ swifter.printSome("https://www.taobao.com",function(param){
+ nativeLog(param);
+ });
+ }
+ };
+ </script>
```
diff --git a/docs/zh/guide/extend/extend-ios-with-swift.md b/docs/zh/guide/extend/extend-ios-with-swift.md
index f4ebeb4..8e63c10 100644
--- a/docs/zh/guide/extend/extend-ios-with-swift.md
+++ b/docs/zh/guide/extend/extend-ios-with-swift.md
@@ -13,14 +13,13 @@
- `WXSwiftTestModule.h/m`中实现
- WXSwiftTestModule.h 中
- ```
- #import <Foundation/Foundation.h>
- #import <WeexSDK/WeexSDK.h>
+ ```swift
+ #import <Foundation/Foundation.h>
+ #import <WeexSDK/WeexSDK.h>
- @interface WXSwiftTestModule : NSObject <WXModuleProtocol>
+ @interface WXSwiftTestModule : NSObject <WXModuleProtocol>
- @end
-
+ @end
```
- WXSwiftTestModule.m 中
@@ -32,36 +31,39 @@
路径具体需要根据自己工程而定
- ```
- #import "WXSwiftTestModule.h"
- #import "WeexDemo-Swift.h" // Swift类和方法 被 `Objective-C` 识别需要导入
+ ```swift
+ #import "WXSwiftTestModule.h"
+ #import "WeexDemo-Swift.h" // Swift类和方法 被 `Objective-C` 识别需要导入
- @implementation WXSwiftTestModule
- #pragma clang diagnostic push //关闭unknow selector的warrning
- #pragma clang diagnostic ignored "-Wundeclared-selector"
+ @implementation WXSwiftTestModule
+ #pragma clang diagnostic push //关闭unknow selector的warrning
+ #pragma clang diagnostic ignored "-Wundeclared-selector"
- WX_EXPORT_METHOD(@selector(printSome:callback:)) //Swift 中定义的方法,XCode 转换成的最终的方法名称,在`WeexDemo-Swift.h`里面查看
+ WX_EXPORT_METHOD(@selector(printSome:callback:)) //Swift 中定义的方法,XCode 转换成的最终的方法名称,在`WeexDemo-Swift.h`里面查看
- #pragma clang diagnostic pop
+ #pragma clang diagnostic pop
- @end
-
+ @end
```
- Swift 中实现
扩展 OC 的类 `WXSwiftTestModule`,增加了一个方法,这个方法就是我们要暴露出来,在 js 中可以调到的
- WXSwiftTestModule.swift
- ```
- import Foundation
- public extension WXSwiftTestModule {
- public func printSome(someThing:String, callback:WXModuleCallback) {
- print(someThing)
- callback(someThing)
- }
- }
+ ```swift
+ import Foundation
+ public extension WXSwiftTestModule {
+ @objc(printSome:callback:)
+ public func printSome(someThing:String, callback:WXModuleCallback) {
+ print(someThing)
+ callback(someThing)
+ }
+ }
```
- `WXSwiftTestModule` 和`WXModuleCallback` 因为是 OC 的,需要在 `WeexDemo-Bridging-Header` 中暴露
+ `WXSwiftTestModule` 和`WXModuleCallback` 因为是 OC 的,需要在 `WeexDemo-Bridging-Header` 中暴露。
+
+ 注意:请在 Swift 文件中为方法添加 `@objc` 修饰符,避免找不到方法的报错。
+
- WeexDemo-Bridging-Header.h中
```
@@ -77,26 +79,25 @@
### module 使用
- 注册 module
- ```
+ ```Objective-C
[WXSDKEngine registerModule:@"swifter" withClass:[WXSwiftTestModule class]];
```
- 前端脚本中用法
- ```
- <template>
- <text>Swift Module</text>
- </template>
+ ```html
+ <template>
+ <text>Swift Module</text>
+ </template>
- <script>
- module.exports = {
- ready: function() {
- var swifter = weex.require('swifter');
- swifter.printSome("https://www.taobao.com",function(param){
- nativeLog(param);
- });
- }
-
- };
- </script>
+ <script>
+ module.exports = {
+ ready: function() {
+ var swifter = weex.require('swifter');
+ swifter.printSome("https://www.taobao.com",function(param){
+ nativeLog(param);
+ });
+ }
+ };
+ </script>
```
\ No newline at end of file