swift で javascript 始めませんか? #iosdc

Post on 16-Apr-2017

4.123 Views

Category:

Software

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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'); }

関数

クラス型

変数

文字列

変数

import JavaScriptCore

let context = JSContext()!

context.evaluateScript("var v1 = 10") context.evaluateScript("var v2 = 20") context.evaluateScript("var v3 = v1 + v2")

let value = context.objectForKeyedSubscript("v3")!

print("Answer = ", value) // Answer = 30

// 指定した名前が存在しない場合は undefined let value = context.objectForKeyedSubscript("vX")!

print("Answer = ", value) // Answer = undefined

let answer = context.evaluateScript("v1 + v2")

let value: JSValue

• toInt32() -> Int32

• toUInt32() -> UInt32

• toDouble() -> Double

• toNumber() -> NSNumber!

• toString() -> String!

• toBool() -> Bool!

• toObject() -> Any!

• toDate() -> Date!

• toArray() -> [Any]!

• toDictionary() -> [AnyHashable : Any]!

• isNumber

• isString

• isBoolean

• isObject

• isUndefined

• isNull

• isArray

• isDate

let article: JSValue = context.evaluateScript("article")!

// プロパティの参照 let title = article.forProperty("title")

// JavaScript メソッドの実行 let note = article.invokeMethod("getDescription", withArguments: [])

let value1: JSValue = context.objectForKeyedSubscript("v1")!

let value2: JSValue = context.objectForKeyedSubscript("v2")!

if value1 == value2 {

}

• parseInt

• parseFloat • encodeURI • decodeURI • encodeURIComponent

• decodeURIComponent

• encodeURI • eval • escape

• Object

• Array • Date • Math • RegEx

• JSON

• Error

context.evaluateScript("var timeout = 5.0")

let account = "@es_kumagai"

// Swift 変数 account を、JavaScript に変数 name で登録 context.setObject(account, forKeyedSubscript: "name" as NSString)

context.evaluateScript( "function getAttribute(name) {" + " return attributes[name];" + "}")

let output : @convention(block) (String) -> Void = {

NSLog("\(account) : \($0)") }

// クロージャーを AnyObject 型にキャストして登録 context.setObject( unsafeBitCast(output, to: AnyObject.self), forKeyedSubscript: "output" as NSString)

context.evaluateScript( "function Article(title, body) {" + " this.title = title;" + " this.body = body;" + "}" + "" + "Article.prototype.getDescription = " + " function() {" + " return this.body.substr(0, 10);" + "};")

@objc protocol ImageInterface : JSExport {

var width: Int { get set } var height: Int { get set }

// インスタンス化をしたい場合は静的メソッドも宣言 static func make(name: String, scale: CGFloat) -> AnyObject }

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)

}

let image = Image(name: "Profile", scale: 0.8)

context.setObject(image, forKeyedSubscript: "icon" as NSString)

context.setObject(Image.self, forKeyedSubscript: "Image" as NSString)

// メソッドは、全てのラベルを名前に含めて実行すること context.evaluateScript( "var banner = " + "Image.makeWithNameScale('picture', 0.5)")

context.evaluateScript( "function Article(title, body) {" + " this.title = title;" + " this.body = body;" + "}" + "" + "Article.prototype.getDescription = " + " function() {" + " return this.body.substr(0, 10);" + "};")

let path = Bundle.main.url( forResource: "JavaScriptAPI", withExtension: "js")! let source = try! String(contentsOf: path)

// 読み込んだ JavaScript をコンテキストで実行

let context = JSContext()!

context.evaluateScript(source)

top related