Trouble with url?.pathExtension

Searching for tif files in my project bundle was only returning one of 5 files using this code:


var pictures = [String]()

let fm = FileManager.default

let path = Bundle.main.resourcePath!

let items = try! fm.contentsOfDirectory(atPath: path)


var url: URL?

for item in items {

url = URL(string: item)

if url?.pathExtension == "tif" {

print("tif found")

print(item)

pictures.append(item)

}

}


These five files are in resource folder of project.

["ID_Dodge Peak_235936_1967_24000_geo.tif", "Base.lproj", "LargeImages", "ID_Benning Mountain_235241_1996_24000_geo.tif", "_CodeSignature", "META-INF", "Frameworks", "ID_Colburn_235747_1996_24000_geo.tif", "Info.plist", "libswiftRemoteMirror.dylib", "ID_Clark Fork_235695_1996_24000_geo.tif", "PkgInfo", "Assets.car", "embedded.mobileprovision", "ID_Clifty_Mountain_235717_1996_24000_geo.tif"]


The search was only finding "ID_Colburn_235747_1996_24000_geo.tif".

After looking at other files, I realized the other file names had spaces, so they weren't being found.

When I added an underscore to complete other names, they were also found.

Question is why?

Answered by Claude31 in 403576022

Add a print and retest with the space in name: you will see url is nil.


    for item in items {
        url = URL(string: item)
        print("item", item, url)     // check if url is nil
        if url?.pathExtension == "tif" {
            print("tif found")
            print(item)
            pictures.append(item)
        }
    }


I suspect the url ibit fails, as descrive in NSURL init documentation:

init(string:)

This method expects

URLString
to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding.


So, space not allowed, should be %20


To comply with this, replace spaces with "%20":

    for item in items {
        url = URL(string: item.replacingOccurrences(of: " ", with: "%20"))
        print("item", item, url)     // check if url is no more nil
        if url?.pathExtension == "tif" {
            print("tif found")
            print(item)
            pictures.append(item)
        }
    }
Accepted Answer

Add a print and retest with the space in name: you will see url is nil.


    for item in items {
        url = URL(string: item)
        print("item", item, url)     // check if url is nil
        if url?.pathExtension == "tif" {
            print("tif found")
            print(item)
            pictures.append(item)
        }
    }


I suspect the url ibit fails, as descrive in NSURL init documentation:

init(string:)

This method expects

URLString
to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding.


So, space not allowed, should be %20


To comply with this, replace spaces with "%20":

    for item in items {
        url = URL(string: item.replacingOccurrences(of: " ", with: "%20"))
        print("item", item, url)     // check if url is no more nil
        if url?.pathExtension == "tif" {
            print("tif found")
            print(item)
            pictures.append(item)
        }
    }

Excellent!

Since these are Forest Service maps downloaded, named as such, I was contemplating how to correct.

Trouble with url?.pathExtension
 
 
Q