【发布时间】:2012-03-03 22:42:15
【问题描述】:
我有一个 NSMutable 字典,其中包含文件 ID 及其文件名+扩展名,格式为 fileone.doc 或 filetwo.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 编程中使用过正则表达式,所以我不完全确定我是否做得对,但我基本上是从 Class Description 页面复制了代码。
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);
我的问题是,有没有更简单的方法来做到这一点?如果不是,我的正则表达式不正确吗?最后,如果我必须使用它,运行时间成本高吗?我不知道用户平均拥有多少文件。
谢谢。
【问题讨论】:
标签: iphone objective-c regex ios5 nsstring