Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!