2014-02-16 61 views
0

即時通訊目前使用過濾文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil]; 
    fileList=[[NSMutableArray alloc]init]; 
    for (NSString *filename in dirContents) { 
     NSString *fileExt = [filename pathExtension]; 
     if ([fileExt isEqualToString:@"png"]) { 

      [fileList addObject:filename]; 
     } 
    } 
    NSLog(@"document folder content list %@ ",fileList); 

標識搜索我的文檔文件夾png格式圖片喜歡讓搜索更加具體,即搜索名爲lorry.png,或car.png例如圖像(簡潔)拉澤rthan讓一切有png格式

我的圖像文件通過apending參考號碼self.certificate.reference到PNG命名的,所以我得到類似car283TYZ.png。這樣我就可以爲創建它的證書使用正確的圖像。

什麼香港專業教育學院試圖

//search folder 
    NSString *certRefstring = [NSString stringWithFormat:@"%@",self.certificate.reference]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil]; 
    fileList=[[NSMutableArray alloc]init]; 
    for (NSString *filename in dirContents) { 
     NSString *fileExt = [filename pathExtension]; 

     if ([fileExt isEqualToString:@"png"]&&[certRefstring rangeOfString:[NSString stringWithFormat:@"%@",self.certificate.reference]].location == NSNotFound) { 

      NSLog(@"string does not contain cert ref %@, file list is %@",self.certificate.reference,fileList); 


     } 
     else{ 

      NSLog(@"string contains cert ref %@ and png files %@",self.certificate.reference,fileList); 

      [fileList addObject:filename]; 

     } 
    } 

    NSLog(@"document folder content list %@ ",fileList); 

這似乎在文件夾中返回的一切。因此,即時尋找一個解決方案,在我.png文件搜索更具體地說/迭代在My Documents文件夾

回答

0

你應該嵌套「如果是

if ([fileExt isEqualToString:@"png"]) 
{ 
    if ([certRefstring rangeOfString:[NSString stringWithFormat:@"%@",self.certificate.reference]].location == NSNotFound) 
    {  
     NSLog(@"string does not contain cert ref %@, file list is %@",self.certificate.reference,fileList); 
    } 
    else 
    { 
     NSLog(@"string contains cert ref %@ and png files %@",self.certificate.reference,fileList); 
     [fileList addObject:filename]; 
    } 
} 
+0

Briliant多數民衆贊成它排序,謝謝 – JSA986

2
NSString *certRefstring = [NSString stringWithFormat:@"%@",self.certificate.reference]; 

所以

[certRefstring rangeOfString:[NSString stringWithFormat:@"%@",self.certificate.reference]].location == NSNotFound 

總是NO。有一個原因,你收到一切。 也許應該改爲

[filename rangeOfString:[NSString stringWithFormat:@"%@",self.certificate.reference]].location == NSNotFound 
+0

您的權利,一切都不同搜索'fileName',與Merlevde結合回答它排序它。謝謝,加1給你! – JSA986