Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda