fileAttributes(atPath:traverseLink:)
Returns a dictionary that describes the POSIX attributes of the file specified at a given.
Declaration
func fileAttributes(atPath path: String, traverseLink yorn: Bool) -> [AnyHashable : Any]?Parameters
- path:
A file path.
- yorn:
If
pathis not a symbolic link, this parameter has no effect. Ifpathis a symbolic link, then:
Return Value
An NSDictionary object that describes the POSIX attributes of the file specified at path. The keys in the dictionary are described in File Attribute Keys. If there is no item at path, returns nil.
Discussion
This code example gets several attributes of a file and logs them.
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *path = @"/tmp/List";
NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:path traverseLink:YES];
if (fileAttributes != nil) {
NSNumber *fileSize;
NSString *fileOwner;
NSDate *fileModDate;
if (fileSize = [fileAttributes objectForKey:NSFileSize]) {
NSLog(@"File size: %qi\n", [fileSize unsignedLongLongValue]);
}
if (fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName]) {
NSLog(@"Owner: %@\n", fileOwner);
}
if (fileModDate = [fileAttributes objectForKey:NSFileModificationDate]) {
NSLog(@"Modification date: %@\n", fileModDate);
}
}
else {
NSLog(@"Path (%@) is invalid.", path);
}As a convenience, NSDictionary provides a set of methods (declared as a category in NSFileManager.h) for quickly and efficiently obtaining attribute information from the returned dictionary: fileGroupOwnerAccountName(), fileModificationDate(), fileOwnerAccountName(), filePosixPermissions(), fileSize(), fileSystemFileNumber(), fileSystemNumber(), and fileType(). For example, you could rewrite the file modification statement in the code example above as:
if (fileModDate = [fileAttributes fileModificationDate])
NSLog(@"Modification date: %@\n", fileModDate);Special Considerations
Because this method does not return error information, it has been deprecated as of OS X v10.5. Use attributesOfItem(atPath:) instead.
See Also
Related Documentation
Deprecated Methods
changeFileAttributes(_:atPath:)fileSystemAttributes(atPath:)directoryContents(atPath:)createDirectory(atPath:attributes:)createSymbolicLink(atPath:pathContent:)pathContentOfSymbolicLink(atPath:)fileManager(_:shouldProceedAfterError:)fileManager(_:willProcessPath:)replaceItemAtURL(originalItemURL:withItemAtURL:backupItemName:options:)