Successfully reported this slideshow.
Your SlideShare is downloading. ×

Swift で JavaScript 始めませんか? #iOSDC

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 62 Ad

Swift で JavaScript 始めませんか? #iOSDC

Download to read offline

Swift ランタイムで簡単に JavaScript を実行できる JavaScriptCore を Swift で使うための基礎のお話です。Swift と JavaScript の相互運用が簡単にできて面白いです。この資料は 2016/08/20 に iOSDC 2016 で発表したものになります。

Swift ランタイムで簡単に JavaScript を実行できる JavaScriptCore を Swift で使うための基礎のお話です。Swift と JavaScript の相互運用が簡単にできて面白いです。この資料は 2016/08/20 に iOSDC 2016 で発表したものになります。

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Similar to Swift で JavaScript 始めませんか? #iOSDC (20)

Advertisement

More from Tomohiro Kumagai (20)

Recently uploaded (20)

Advertisement

Swift で JavaScript 始めませんか? #iOSDC

  1. 1. for (var index in lines) { var line = lines[index]; var itemContent; if (line.match(expression)) { itemContent = RegExp.$2; } var outputItem = core.escapeHtml(itemContent); return outputItems.map(convertToTag).join('n'); }
  2. 2. 
 

  3. 3. #iOSDC
  4. 4. 関数 クラス型 変数 文字列 変数
  5. 5. 
 

  6. 6. import JavaScriptCore
  7. 7. let context = JSContext()!
  8. 8. context.evaluateScript("var v1 = 10") context.evaluateScript("var v2 = 20") context.evaluateScript("var v3 = v1 + v2")
  9. 9. let value = context.objectForKeyedSubscript("v3")! print("Answer = ", value) // Answer = 30 // 指定した名前が存在しない場合は undefined let value = context.objectForKeyedSubscript("vX")! print("Answer = ", value) // Answer = undefined
  10. 10. let answer = context.evaluateScript("v1 + v2")
  11. 11. let value: JSValue
  12. 12. •toInt32() -> Int32 •toUInt32() -> UInt32 •toDouble() -> Double •toNumber() -> NSNumber! •toString() -> String! •toBool() -> Bool! •toObject() -> Any! •toDate() -> Date! •toArray() -> [Any]! •toDictionary() -> [AnyHashable : Any]!
  13. 13. •isNumber •isString •isBoolean •isObject •isUndefined •isNull •isArray •isDate
  14. 14. let article: JSValue = context.evaluateScript("article")! // プロパティの参照 let title = article.forProperty("title") // JavaScript メソッドの実行 let note = article.invokeMethod("getDescription", withArguments: [])
  15. 15. let value1: JSValue = context.objectForKeyedSubscript("v1")! let value2: JSValue = context.objectForKeyedSubscript("v2")! if value1 == value2 { }
  16. 16. •parseInt •parseFloat •encodeURI •decodeURI •encodeURIComponent •decodeURIComponent •encodeURI •eval •escape •Object •Array •Date •Math •RegEx •JSON •Error
  17. 17. context.evaluateScript("var timeout = 5.0")
  18. 18. let account = "@es_kumagai" // Swift 変数 account を、JavaScript に変数 name で登録 context.setObject(account, forKeyedSubscript: "name" as NSString)
  19. 19. context.evaluateScript( "function getAttribute(name) {" + " return attributes[name];" + "}")
  20. 20. let output : @convention(block) (String) -> Void = { NSLog("(account) : ($0)") } // クロージャーを AnyObject 型にキャストして登録 context.setObject( unsafeBitCast(output, to: AnyObject.self), forKeyedSubscript: "output" as NSString)
  21. 21. context.evaluateScript( "function Article(title, body) {" + " this.title = title;" + " this.body = body;" + "}" + "" + "Article.prototype.getDescription = " + " function() {" + " return this.body.substr(0, 10);" + "};")
  22. 22. @objc protocol ImageInterface : JSExport { var width: Int { get set } var height: Int { get set } // インスタンス化をしたい場合は静的メソッドも宣言 static func make(name: String, scale: CGFloat) -> AnyObject }
  23. 23. class Image : NSObject, ImageInterface { var width, height: Int required init(name: String, scale: CGFloat) {…} class func make(name: String, scale: CGFloat) -> AnyObject { return self.init(name: name, scale: scale) }
  24. 24. let image = Image(name: "Profile", scale: 0.8) context.setObject(image, forKeyedSubscript: "icon" as NSString)
  25. 25. context.setObject(Image.self, forKeyedSubscript: "Image" as NSString) // メソッドは、全てのラベルを名前に含めて実行すること context.evaluateScript( "var banner = " + "Image.makeWithNameScale('picture', 0.5)")
  26. 26. context.evaluateScript( "function Article(title, body) {" + " this.title = title;" + " this.body = body;" + "}" + "" + "Article.prototype.getDescription = " + " function() {" + " return this.body.substr(0, 10);" + "};")
  27. 27. let path = Bundle.main.url( forResource: "JavaScriptAPI", withExtension: "js")! let source = try! String(contentsOf: path) // 読み込んだ JavaScript をコンテキストで実行 let context = JSContext()! context.evaluateScript(source)

×