How to export method of inner class using JSExport
Asked Answered
B

1

7

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 as the following in console,

Hello Luitel sdf ❌ Error in Javascript: TypeError: undefined is not a function (evaluating 'Person1.testProperty.getNNN()') undefined

i want to access the method getNNN() of Personal Class. Here, if getNNN() is the method of OuterClassPersonal class and OuterClassPersonal implements PersonalJavaScriptMethod, it works properly.

The Question is, How can i access method of internal class "Personal" from Javascript?

Beckham answered 8/2, 2016 at 8:40 Comment(0)
B
2

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.

Beckham answered 17/2, 2016 at 3:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.