Ambigious reference to member 'fileManager'(_shouldProceedAfterError)

I can't seem to figure out how to solve this error.

I'm getting it from this line:


let allImageDirectories = try! fileManager.contentsOfDirectory(atPath: documentDirectory)




Full Function:


    func createArrayData()->[FolderCell]{
                    var folderCells: [FolderCell] = []
                    let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.path
                    let allImageDirectories =  try! fileManager.contentsOfDirectory(atPath: documentDirectory)
                    let imageDirectories = allImageDirectories[1...(allImageDirectories.count-1)]
                    
                    for directory in imageDirectories{
                        
                        
                    let imageDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(directory, isDirectory: true)
                    let allImages =  try! fileManager.contentsOfDirectory(atPath: imageDirectory.path)
                    let displayImage =  UIImage( named: allImages[0])
                        
                        folderCells.append(FolderCell(image: displayImage!, label :directory))
                    return folderCells
                    }

Accepted Reply

Swift compiler outputs Ambiguous reference to member, when it cannot choose one appropriate member.


So, where is your declaration of `fileManager`? (Please do not forget, Swift identifiers are case-sensitive.)


Maybe you may have mixed up some different codes and one of them defined `fileManger` but you have missed it.


Try replacing all occurences of `fileManager` (I repeat, case-sensitive) to `FileManager.default`.


For example, your line 04.

let allImageDirectories =  try! FileManager.default.contentsOfDirectory(atPath: documentDirectory) 


(You should better reconsider if it is appropriate to use `try!` here.)

Replies

Swift compiler outputs Ambiguous reference to member, when it cannot choose one appropriate member.


So, where is your declaration of `fileManager`? (Please do not forget, Swift identifiers are case-sensitive.)


Maybe you may have mixed up some different codes and one of them defined `fileManger` but you have missed it.


Try replacing all occurences of `fileManager` (I repeat, case-sensitive) to `FileManager.default`.


For example, your line 04.

let allImageDirectories =  try! FileManager.default.contentsOfDirectory(atPath: documentDirectory) 


(You should better reconsider if it is appropriate to use `try!` here.)

I see. I was trying to acess the buit-in fileManager, and was confused as to why there were two types: fileManager and FileManager.

I will see if I must have mixed up the variables.


Thank you.



EDIT: It turns out I did mix up some variables. Thank you so much.

Also, just as an aside, many frameworks, including

FileManager
, have older APIs that work with paths and newer APIs that work with URLs. You’ll find that things go smoother if you work either with URLs or with paths, rather than bouncing between the two.

For example,

FileManager
has a
contentsOfDirectory(at:includingPropertiesForKeys:options:)
that takes a
URL
, so you can call it with the result from
urls(for:in:)
without having to do a conversion.

Moreover, I generally recommend that folks work with URLs because they represent the more modern API.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"