1 year ago
#318998
John Daschbach
Swift FileManager attributesOfItem convenience functions not working
The documentation for the FileManager function attributesOfItem(atPath:) says the following:
"As a convenience, NSDictionary provides a set of methods (declared as a category on NSDictionary) for quickly and efficiently obtaining attribute information from the returned dictionary: fileGroupOwnerAccountName(), fileModificationDate(), fileOwnerAccountName(), filePosixPermissions(), fileSize(), fileSystemFileNumber(), fileSystemNumber(), and fileType()."
But trying to use these convenience methods (which I thought would be "extension" but Apple is calling them "category") does not work.
let fm2 = FileManager.default
let lPath = "/Users/Guest"
do {
let aOI1 = try fm2.attributesOfItem(atPath: lPath)
// aOI1 is a dictionary
if let fS = aOI1[FileAttributeKey.size] {
print("FileSize ",fS)
}
// The following does not compile
//
// Error: Value of type '[FileAttributeKey:Any]' has no member 'fileSize'
//let fS2 = aOI1.fileSize()
if let pP = aOI1[FileAttributeKey.posixPermissions] {
print("posix ",pP)
}
// The following does not compile
// Error: "This method is defined on NSDictionary, and may not
// be available in this context"
//let pP2 = aOI1.filePosixPermission()
} catch {
print("Error")
}
I'm using XCode 13.3 with Swift 5.6.
Obviously I'm missing something here. The Apple documentation for attributesOfItem(atPath: lPath)
specifically notes the convenience methods
defined on NSDictionary but although they exist I can't seem to call them
successfully. Perhaps it is better to just use the FileAttributeKey.???
but I wonder how the Apple supplied "category methods" work.
swift
macos
extension-methods
foundation
0 Answers
Your Answer