![](/img/trans.png)
[英]How do I cast my NSArray to List<UIView> in xamarin ios
[英]How do I remove the file extension from a list of files in an NSArray in iOS 5
在iOS 5中工作時,我已經從文檔目錄中讀取了文件名列表,該數組稱為“文件列表”。 我正在嘗試獲取不帶擴展名的文件名列表。 僅列表中的姓氏已刪除擴展名。 有任何想法嗎?
- (IBAction)getFile:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory =[paths objectAtIndex:0];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *names = [[[fileList valueForKey:@"description"]componentsJoinedByString:@"\n"]stringByDeletingPathExtension];
NSLog(@"File Name Is \n%@",names);
showFile.text = names;
}
- (IBAction)getFile:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory =[paths objectAtIndex:0];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *names = nil;
for (NSString *name in fileList){
if (!names) names = [[name lastPathComponent] stringByDeletingPathExtension];
else names = [names stringByAppendingFormat:@" %@",[[name lastPathComponent] stringByDeletingPathExtension]];
}
NSLog(@"File Name Is \n%@",names
}
看起來您正在使用數組的描述來獲取完整的數組內容,然后正在刪除整個文件的擴展名,而不是從每個單獨的文件中刪除。 嘗試先刪除文件擴展名:
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:[fileList count]];
[fileList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[newArray addObject:[obj stringByDeletingPathExtension]];
}];
NSString *names = [newArray componentsJoinedByString:@"\n"];
showFile.text = names;
enumerateObjectsUsingBock
方法遍歷數組中的每個項目。 在代碼塊中,獲取該對象,刪除路徑擴展,然后將其添加到新數組中。 在處理完整個數組之后,您可以使它們使用componentsJoinedByString
在每個文件名之間添加換行符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.