Here is what I want to achieve. 1. Searching all the files 2. find all .jpg files during the searching 3. save all .jpg file paths into NSMutableArray
Here are the codes:
Created the NSMutableArray:
NSMutableArray *jpgFiles = [[[NSMutableArray alloc]init]autorelease];
Searching all the parent folders under (/Users/) path (Start NSThread in here):
NSString* filePath = [url path]; NSArray *dirFiles = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:filePath error:nil]; if([dirFiles count]!=0) { for (int j=0; j<[dirFiles count]; j++) { NSString* pathExtension = [[dirFiles objectAtIndex:j] pathExtension]; //if extension is null, we forwards to next level. if ([pathExtension isEqualTo:@""]) { @autoreleasepool { [NSThread detachNewThreadSelector:@selector(searchingPicture:) toTarget:self withObject:[filePath stringByAppendingPathComponent:[dirFiles objectAtIndex:j]]]; } } else { //if find jpg in this level, save into array if([pathExtension isEqualTo:@"JPG"]) { [jpgFiles addObject:[filePath stringByAppendingPathComponent:[dirFiles objectAtIndex:j]]]; } } } }
Keep searching the rest of sub folders and save proper file path into array:
-(void)searchingPicture:(NSString*)path { NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; NSURL *directoryURL = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey]; NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:directoryURL includingPropertiesForKeys:keys options:0 errorHandler:^(NSURL *url, NSError *error) { // Handle the error. // Return YES if the enumeration should continue after the error. return YES; }]; for (NSURL *url in enumerator) { NSError *error; NSNumber *isDirectory = nil; if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) { // handle error } else if (! [isDirectory boolValue]) { // No error and it’s not a directory; do something with the file if([[[url path] pathExtension]isEqualTo:@"JPG"]) { //This line gives me error !!! [jpgFiles addObject:[url path]]; } } } }
Error: (At beginning, it works fine and save many different files into array, but after saved around 50 files it starts to give me error and crash at the end).
Here is the correct element adds into array:
/Users/NAME/Documents/Graduate Books/IMG_2517.JPG
Here is the error message:
-[NSPathStore2 addObject:]: unrecognized selector sent to instance 0x10011d4d0
However, even this error occurs, it still keeps saving some of paths into array and then it will throw another error:
An uncaught exception was raised
Could you guys tell me how to fix it?? Thanks !!