Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
How I can return result of js or any function to the screen of iPhone? by ContentView
    //  ContentView.swift
//  Shared

import Foundation
import SwiftUI
import JavaScriptCore

class cube {
    var result: String
    
    func do_js(text: String) -> String {
        
let jsSource = "var testFunct = function(message) { return \"Test Message: \" + message;}"

var context = JSContext()
context?.evaluateScript(jsSource)

let testFunction = context?.objectForKeyedSubscript("testFunct")
        var result = testFunction?.call(withArguments: [text]).toString()
    return result!
    }
}


struct ContentView: View {
    
    cube().do_js(text: "Hello world") // Starts forom here
    
    var show_text = lol().result
    
    var body: some View {
        Text(show_text)
            .font(.body)
            .fontWeight(.black)
            .foregroundColor(Color.red)
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

(Sorry, I'm beginner and come to swift even not from js, but from python! So it's incredibly new for me. But js more understandable for me from python.)

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here's the simplest version. In this version, it basically uses your original pattern where doJS returns a value. The disadvantage to this version is that doJS will get called every time the view renders.

class Cube {
    func doJS(text: String) -> String? {
        let jsSource = "var testFunct = function(message) { return \"Test Message: \" + message;}"

        let context = JSContext()
        context?.evaluateScript(jsSource)

        let testFunction = context?.objectForKeyedSubscript("testFunct")
        return testFunction?.call(withArguments: [text]).toString()
    }
}

struct ContentView: View {
    var body: some View {
        Text(Cube().doJS(text: "Hello, world!") ?? "No result")
            .font(.body)
            .fontWeight(.black)
            .foregroundColor(Color.red)
            .padding()
    }
}

And here's a slightly different version. In this version, Cube is an ObservableObject with a @Published value that stores the result. It will only get called once in onAppear.

class Cube : ObservableObject {
    @Published var result : String?
    
    func doJS(text: String) {
        let jsSource = "var testFunct = function(message) { return \"Test Message: \" + message;}"

        let context = JSContext()
        context?.evaluateScript(jsSource)

        let testFunction = context?.objectForKeyedSubscript("testFunct")
        result = testFunction?.call(withArguments: [text]).toString()
    }
}

struct ContentView: View {
    @StateObject var cube = Cube()
    
    var body: some View {
        Text(cube.result ?? "No result")
            .font(.body)
            .fontWeight(.black)
            .foregroundColor(Color.red)
            .padding()
            .onAppear {
                cube.doJS(text: "Hello, world!")
            }
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda