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

283
Views
¿Cómo puedo devolver el resultado de js o cualquier función a la pantalla del iPhone? por 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() } }

(Lo siento, soy principiante y llegué a Swift incluso no desde js, ¡sino desde python! Así que es increíblemente nuevo para mí. Pero js es más comprensible para mí desde python).

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aquí está la versión más simple. En esta versión, básicamente usa su patrón original donde doJS devuelve un valor. La desventaja de esta versión es que se llamará a doJS cada vez que se muestre la vista.

 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() } }

Y aquí hay una versión ligeramente diferente. En esta versión, Cube es un ObservableObject con un valor @Published que almacena el resultado. Solo se llamará una vez en 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!