Sunil luitel February 2016

How to export method of inner class using JSExport

I have one class whose methods and properties are exported using JSExport. In normal cases, it works well. But if the property is the type of some inner class, then it can not be accessed from javascript.

Following are the classes i am using:

Person.swift

import Foundation
import JavaScriptCore


@objc
protocol PersonJavaScritMethod : JSExport {
    var testProperty : OuterClassPersonal.Personal? { get set }
    func sayHello(name1:String)
}
class Person : NSObject, PersonJavaScritMethod {
    var name : String!
     var testProperty:OuterClassPersonal.Personal?

    init(name:String) {
        testProperty = OuterClassPersonal.Personal()
        super.init()
        self.name = name
    }

    class func create(name : String) -> Person {
        return Person(name: name)
    }

    func sayHello(name1:String) {
        println("Hello \(name) \(name1)")
    }
}

Personal.swift

import Foundation
import JavaScriptCore

@objc
protocol PersonalJavaScritMethod : JSExport {
     func getNNN()
}

class OuterClassPersonal:NSObject,JSExport{


    class Personal:NSObject,PersonalJavaScritMethod {
        func getNNN(){
            println("Hip Hip Hurray")
        }
    }
}

JavaScript.swift

import Foundation
import JavaScriptCore
class Javascript {


    let context = JSContext()

    func evaluateScript() {
        context.exceptionHandler = { context, exception in
            println("❌ Error in Javascript: \(exception) ");
        }

        var  p1:Person = Person(name: "Luitel")
        context.globalObject.setObject(p1, forKeyedSubscript: "Person1")
        context.evaluateScript("Person1.sayHello('sdf') \n ")

        var result =  context.evaluateScript("Person1.testProperty.getNNN() \n")
        println("\(result.toString())")
    }

}

When i run the evaluateScript() method, i get the output a

Answers


Sunil luitel February 2016

Seems there is no way exporting inner class functions to javascript. I moved the inner class (Personal) out and created independent class and it worked.

Post Status

Asked in February 2016
Viewed 2,196 times
Voted 5
Answered 1 times

Search




Leave an answer