我有一个NSMutable字典,它以简单的fileone形式包含文件ID及其文件名+扩展名.doctor 或文件二.pdf.我需要确定文件的类型,以便在我的UITableView中正确显示相关图标.以下是我到目前为止所做的.
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编程中使用过regex,所以我不能完全确定我是否做得对,但我基本上是从类描述页面复制代码的.
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);
我的问题是,有没有更简单的方法?如果不是,我的正则表达式是否不正确?最后,如果我必须使用它,它在运行时会很昂贵吗?我不知道用户的平均文件量是多少.
非常感谢.