I'm picking random files in folders that might have thousands of files. The
only way I can see to do this in Cocoaland is to get an array of the entire
contents of the folder (NSFileManager contentsOfDirectoryAtPath:error:), then
choose from that array by index. Isn't this terribly wasteful of CPU and memory?
The next best thing I could come up with was to iterate through until I got to
my index. It seems less costly than the above for lower indices, but possibly
more costly for higher indices.
NSDirectoryEnumerator* enumer = [fileMan enumeratorAtPath:foldPath];
NSString* path;
unsigned long n = SSRandomUnsignedLongBetween(0,
folderItem.numItems);
// Loop until our index:
for(i = 0; i < n; i++) {
path = [enumer nextObject];
[enumer skipDescendants];
}
imagePath = [foldPath stringByAppendingFormat:@"/%@", path];
And before anyone points out the uninitted path variable if n is 0, it will
never be 0 due to empty folders being filtered out.
Is there any better way to get an indexed file? Ya know, like we could easily
do in Carbon with something like PBGetCatInfoSync by supplying the index in
pb.dirInfo.ioFDirIndex?
--
Steve Mills
Drummer, Mac geek
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]