問題描述
我有一個 NSMutable 字典,其中包含文件 ID 及其文件名+擴展名,格式為 fileone.doc 或 filetwo.pdf.我需要確定在我的 UITableView 中正確顯示相關圖標的文件類型.這是我到目前為止所做的.
I have an NSMutable dictionary that contains file IDs and their filename+extension in the simple form of fileone.doc or filetwo.pdf. I need to determine what type of file it is to correctly display a related icon in my UITableView. Here is what I have done so far.
NSString *docInfo = [NSString stringWithFormat:@"%d", indexPath.row]; //Determine what cell we are formatting
NSString *fileType = [contentFiles objectForKey:docInfo]; //Store the file name in a string
我編寫了兩個正則表達式來確定我正在查看的文件類型,但它們從未返回肯定的結果.我之前沒有在 iOS 編程中使用過正則表達式,所以我不完全確定我是否做得對,但我基本上是從 Class Description 頁面復制了代碼.
I wrote two regex to determine what type of file I'm looking at, but they never return a positive result. I haven't used regex in iOS programming before, so I'm not entirely sure if I'm doing it right, but I basically copied the code from the Class Description page.
NSError *error = NULL;
NSRegularExpression *regexPDF = [NSRegularExpression regularExpressionWithPattern:@"/^.*\.pdf$/" options:NSRegularExpressionCaseInsensitive error:&error];
NSRegularExpression *regexDOC = [NSRegularExpression regularExpressionWithPattern:@"/^.*\.(doc|docx)$/" options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numMatch = [regexPDF numberOfMatchesInString:fileType options:0 range:NSMakeRange(0, [fileType length])];
NSLog(@"How many matches were found? %@", numMatch);
我的問題是,有沒有更簡單的方法來做到這一點?如果不是,我的正則表達式不正確嗎?最后,如果我必須使用它,運行時間成本高嗎?我不知道用戶平均擁有多少文件.
My questions would be, is there an easier way to do this? If not, are my regex incorrect? And finally if I have to use this, is it costly in run time? I don't know what the average amount of files a user will have will be.
謝謝.
推薦答案
你在找[fileType pathExtension]
NSString 文檔:pathExtension
這篇關于獲取包含在 NSString 中的文件的擴展名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!