guard let result = try? await evaluateJavaScript Crash

myCode is here

// titleScript = "document.querySelector('#\(rawValue) span')?.textContent"
guard let titleResult = try? await webView.evaluateJavaScript(type.titleScript),
      let title = titleResult as? String else { return }

this code has error

Thread 1: Swift runtime failure: Unexpectedly found nil while implicitly unwrapping an Optional value

but edit Code like this It is works Successful

do {
    ...
    let titleResult = try await webView.evaluateJavaScript(type.titleScript)
    let title = titleResult as? String
    ...
} catch {
    LogManager.log(level: .error, self, #function, error, "title is Invalid : \(type.titleScript)")
    continue
}

I don't know why guard let _ = try? is Fail

Answered by DTS Engineer in 839577022

It’s better to reply as a reply, rather than in the comments; see Quinn’s Top Ten DevForums Tips for this and other titbits.

My advice here is that you pull this code out into a small test project and confirm the issue there. That might help you isolate what’s going on but, if you still can’t figure it out, post a URL [1] to download your test project and I’d be happy to take a look.

IMPORTANT I’m looking for a small test project, similar to the example I posted above. If you post something that’s large, or relies on third-party tools or libraries, I won’t have time to look at it.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] If you have troubles posting your URL, do that in the clear, per tip 14.

Is it failing on let titleResult or let title?

Try splitting the guard statement into two and see which one it fails on.

It's a good idea to separate those two statements because the second one is relying on the result of the first one, and they are likely being evaluated at the same time.

It's a good idea to separate those two statements

Indeed. There’s a lot of stuff in play here and it’s best to make things explicit.

I’m not actually sure why things are trapping though. Given that everything is explicitly unwrapped, it should return nil rather than trap.

And I tried running your code here in my office and it didn’t trap. Specifically, I put the code below in a macOS app and it didn’t trap in any of the titleScript cases.

This is Xcode 16.3 on macOS 15.4.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"


import Cocoa
import WebKit

@main
final class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var webView: WKWebView!

    …
    
    @MainActor
    func test() async {
        print("will run")
        // let titleScript = "['Hello', 'Cruel', 'World!']"
        // let titleScript = "'Hello Cruel World!'"
        let titleScript = "null"
        // let titleScript = "."
        guard
            let titleResult = try? await webView.evaluateJavaScript(titleScript),
            let title = titleResult as? String
        else {
            print("did not run")
            return
        }
        print("did run, title: \(title)")
    }
    
    @IBAction
    func testAction(_ sender: Any) {
        Task {
            await test()
        }
    }
}

It’s better to reply as a reply, rather than in the comments; see Quinn’s Top Ten DevForums Tips for this and other titbits.

My advice here is that you pull this code out into a small test project and confirm the issue there. That might help you isolate what’s going on but, if you still can’t figure it out, post a URL [1] to download your test project and I’d be happy to take a look.

IMPORTANT I’m looking for a small test project, similar to the example I posted above. If you post something that’s large, or relies on third-party tools or libraries, I won’t have time to look at it.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] If you have troubles posting your URL, do that in the clear, per tip 14.

guard let result = try? await evaluateJavaScript Crash
 
 
Q