if within let from return func ?

I'm sure I'm not too far off what I want but doing this the openPanel never fires. Looked with Google but can't find an example of what I'm trying to achieve ?


Using ...

let local_path = {

let return_path = self.openDir()!.path

if return_path == "" {

print("Return URL nil")

} else {

print("Return URL: \(return_path)")

}

}


openDir is NSOpenPanel which returns URL?

Accepted Reply

It's not clear why you're writing code like this. This construct:


     let local_path = { … }


sets the value of "local_path" to a closure. It doesn't execute any lines of code.


Are you in fact intending to create a closure?

Replies

Just looked over my post and realised I'm not even setting the variable, just a test print out. 😮

It's not clear why you're writing code like this. This construct:


     let local_path = { … }


sets the value of "local_path" to a closure. It doesn't execute any lines of code.


Are you in fact intending to create a closure?

Thanks, I think a closure is probably closest to what i have in mind. Mainly trying to not have logic after but rather process it at the time the variable is set.

It's unclear to me what you're actually trying to do. If you have a closure, you're going to have to invoke it sometime, but your closure (as it currently stands) doesn't have any result, and doesn't have any useful side-effects other than printing. Invoking it isn't really going to do anything.


>>Mainly trying to not have logic after but rather process it at the time the variable is set.


"Logic after" what? What "it" is going to be processed? Which variable "is set"? 🙂

I know you know what you mean, but you aren't giving us much to go on here.

Instead of using a closure I tried bit simpler, also according to your post ...


        let local_path: String = {
            let open_result = self.openDir()
            if (open_result != nil) {
                return "Got path ... " + open_result!.path
            } else {
                return "Path nil"
            }
        }()

        print("Local Path: \(local_path)")


The print of local_path is always "Path nil" and prints before openPanel has finished choosing. Any idea why this is ?

Also does the same with ...


        if let local_path = openDir()?.path {
            print("Path: \(local_path)")
        } else {
            print("Path nil")
        }


After looking at ... https://docs.swift.org/swift-book/LanguageGuide/OptionalChaining.html


Always prints "Path nil".

My bad, it was the function's return had slightly wrong.