1

It's been about years since I've done any work in macOS and more than two since I've touched Swift. I'm running myself in circles on this:

I have two functions, the first contains a while loop to check the value within a dictionary returned by the second. :

func checkPath(_ strPath:String) { 

...
while bIsDesktop == false {
    let dictResults:Dictionary<String, Any> = checkPath(arrPath)

    if dictResults["isDesktop"] { //-->I'm getting crushed here
        //do something with returned array
    }

}

private func checkPath(_ arrPath:Array<String>) -> Dictionary<String, Any> {
    var bIsDesktop = false
    var arrPath = arrPath
    if arrPath[0] != "Desktop" {
       arrPath.removeFirst()
    } else {
       bIsDesktop = true
    }

    return ["paths":arrPath, "isDesktop":bIsDesktop]
}

arrPath is an array of strings (file path items)

I know the returned dictionary is like so [String:Any]

Problem is no matter what I do I run into some sort of compiler error like:

Optional type 'Any?' cannot be used as a boolean; test for '!= nil' instead

In the grander scheme of things, in case there's a more elegant solution, what I am trying to do is strip the path to a folder selected by the user of the ///File:User/username/Desktop/... and just have Desktop/... If there's a better solution than what I am attempting I'd appreciate seeing it but if you could also explain why I can't get the optional out of the dictionary I'd appreciate that as well.

I've tried:

guard let bIsDesktop = dictResults["isDesktop" as Bool else { ...
if dictResults?["isDesktop"]...

and various other permutations. Either I get nil or a compiler error.

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • 1
    Look up "conditional binding". More generally, read the Swift language guide, cover to cover. https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html – Alexander Nov 11 '19 at 22:00
  • I honestly don't know what you are trying to do just by reading your code. So you want to delete everything before `Desktop`? What if the path does not have `Desktop`? – Sweeper Nov 11 '19 at 22:03
  • 1
    You shouldn't be using `String` paths for file handling. Use `URL`s instead, which have helper properties/methods, such as `lastPathComponent` that will come in handy for your goals. – Dávid Pásztor Nov 11 '19 at 22:06
  • @DávidPásztor better to get the URL resource values (localizedname) https://stackoverflow.com/questions/28570627/how-to-find-file-uti-for-file-withouth-pathextension-in-a-path-in-swift/34772517?r=SearchResults&s=2|31.8892#34772517 – Leo Dabus Nov 11 '19 at 22:10

1 Answers1

0

I do think you ran into a complex error and I didn't test your entire story. My apologies for that and still trying to post this as a solution.

To my first knowledge, you are trying to see if a variable is true or not with the statement:

if dictResults["isDesktop"] { //-->I'm getting crushed here
        //do something with returned array
    }

The problem is that you know, or imply, that the dictionary entry at dictResult["isDesktop"] is a boolean. However, your compiler doesn't know, since your provided it with the knowledge it can be of type "Any".

This doesn't't sit well with the compiler, since it cannot determine that an "Any" type is either true or false.

As far as I understand, you first have to cast dictResult["isDesktop"] to a boolean before you can ask the compiler to evaluate it to be true or false. If you don't cast it to a boolean, you have to treat it as a none boolean type and tell the compiler in the if-statement to evaluate it to the value you want.

The compiler error is basically telling you: "I don't know what type Any? is, but I know it can be some value or none. If you put in a statement "if (type), I assume you want me to evaluate this optional to have a value or not. This is not what you want, you want to tell the compiler that it needs to evaluate the statement as a bool.

I think either should work:

if let result = dictResults["isDesktop"] as? bool {

}

or

if (dictResults["isDesktop"] as bool) {

}

Again, I haven't tested it and I'm sorry. I do think this is the solution to this problem.

Kind regards, MacUserT

MacUserT
  • 1,760
  • 2
  • 18
  • 30