ReferenceError: Не удается найти переменную: jQuery при использовании с swift?
Я использую JavaScript в Swift. Я использую JavaScriptCore для этого. Я создал зазубренный файл JS и загружаю его. Ниже приведен код для файла JS.
function setOldCities(appCities) {
var city_array = appCities.split(',');
for (var i = 0; i < city_array.length; i++) {
// Trim the excess whitespace.
city_array[i] = city_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
jQuery('.map-pin').each(function() {
if (($(this).attr('city') == city_array[i])) {
$(this).addClass("active");
try {
webkit.messageHandlers.callFromActivity.postMessage();
} catch(err) {
console.log('The native context does not exist yet');
}
}
});
}
}
Я вызываю эту функцию из класса swift. Функция вызывается, но она не распознает, что такое jQuery.
Код для вызова функции из swift.
func callJsFunction()
{
if let functionFullname = self.jsContext.objectForKeyedSubscript("setOldCities") {
// Call the function that composes the fullname.
filter = "City1,City2";
if let fullname = functionFullname.call(withArguments: [filter]) {
print("JS RESULT \(fullname)")
}
}
}
Снизу кода я загружаю файл JS в быстрый контекст.
func initializeJS() {
self.jsContext = JSContext()
// Add an exception handler.
self.jsContext.exceptionHandler = { context, exception in
if let exc = exception {
print("JS Exception:", exc.toString())
}
}
// Specify the path to the jssource.js file.
if let jsSourcePath = Bundle.main.path(forResource: "maps", ofType: "js", inDirectory: "map/js") {
do {
// Load its contents to a String variable.
let jsSourceContents = try String(contentsOfFile: jsSourcePath)
// Add the Javascript code that currently exists in the jsSourceContents to the Javascript Runtime through the jsContext object.
self.jsContext.evaluateScript(jsSourceContents)
}
catch {
print("error is \(error.localizedDescription)")
}
}
let consoleLogObject = unsafeBitCast(self.consoleLog, to: AnyObject.self)
self.jsContext.setObject(consoleLogObject, forKeyedSubscript: "consoleLog" as (NSCopying & NSObjectProtocol))
_ = self.jsContext.evaluateScript("consoleLog")
}
Классmap-pin находится в отдельном html-файле. Я включил maps.js в HTML-файл