ios - What is the least resource-intensive way to know whether the object is a folder? -
i need sort 8145 objects (there may more) files , folders. (tested on a8x).
first method.
for(nsstring *filename in fileslist) //this method takes average of 7.7 seconds. { fileextension = (__bridge cfstringref)[filename pathextension]; fileuti = uttypecreatepreferredidentifierfortag(kuttagclassfilenameextension, fileextension, null); if ([[[[nsfilemanager defaultmanager] attributesofitematpath:[folderpath stringbyappendingpathcomponent:filename] error:nil] filetype] isequaltostring:@"nsfiletypedirectory"] && !uttypeconformsto(fileuti, kuttypertfd)) [onlyfolderlist addobject:filename]; else [onlyfileslist addobject:filename]; }
second method.
for(nsstring *filename in fileslist) //this method takes average of 2.9 seconds. { fileextension = (__bridge cfstringref)[filename pathextension]; fileuti = uttypecreatepreferredidentifierfortag(kuttagclassfilenameextension, fileextension, null); [[nsfilemanager defaultmanager] fileexistsatpath:[folderpath stringbyappendingpathcomponent:filename] isdirectory:&isdir]; if (isdir && !uttypeconformsto(fileuti, kuttypertfd)) [onlyfolderlist addobject:filename]; else [onlyfileslist addobject:filename]; }
do know faster way?
-----update add fastest method----- // 0.26 seconds 8145 objects!
nsnumber *isdirectory; nserror *error = nil; nsstring *name; nsarray *fileslist = [[nsfilemanager defaultmanager] contentsofdirectoryaturl:[nsurl fileurlwithpath:folderpath] includingpropertiesforkeys:[nsarray arraywithobjects:nsurlisdirectorykey,nsurlnamekey,nil] options:nsdirectoryenumerationskipshiddenfiles error:nil]; (nsurl *url in fileslist) { [url getresourcevalue:&name forkey:nsurlnamekey error:&error]; [url getresourcevalue:&isdirectory forkey:nsurlisdirectorykey error:&error]; if ([isdirectory boolvalue]) { cfstringref fileextension = (__bridge cfstringref)[name pathextension]; cfstringref fileuti = uttypecreatepreferredidentifierfortag(kuttagclassfilenameextension, fileextension, null); if (!uttypeconformsto(fileuti, kuttypertfd)) [onlyfolderlist addobject:name]; else [onlyfileslist addobject:name]; } else [onlyfileslist addobject:name]; }
try using contentsofdirectoryaturl:includingpropertiesforkeys:options:error:
:
nsurl *root = ... // root url file list nsfilemanager *fm = [nsfilemanager defaultmanager]; nserror *error = nil; nsarray *fileslist = [fm contentsofdirectoryaturl:root includingpropertiesforkeys:@[ nsurlisdirectorykey, nsurltypeidentifierkey ] options:nsdirectoryenumerationskipshiddenfiles error:&error]; if (dirurls) { (nsurl *fileurl in fileslist) { nsstring *filename = [[fileurl path] lastpathcomponent]; nsnumber *isdir = nil; [fileurl getresourcevalue:&isdir forkey:nsurlisdirectorykey error:nil] if ([isdir boolvalue]) { nsstring *uti; [fileurl getresourcevalue:&uti forkey:nsurltypeidentifierkey error:nil]; if ([(__bridge nsstring *)kuttypertfd isequaltostring:uti]) { [onlyfileslist addobject:filename]; } else { [onlyfolderslist addobject:filename]; } } else { [onlyfileslist addobject:filename]; } } } else { nslog(@"unable list of files in %@: %@", root, error); }
Comments
Post a Comment