相关疑难解决方法(0)

获取NSString中包含的文件的扩展名

我有一个NSMutable字典,其中包含fileone.doc或filetwo.pdf的简单形式的文件ID及其文件名+扩展名.我需要确定在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
Run Code Online (Sandbox Code Playgroud)

我写了两个正则表达式来确定我正在查看的文件类型,但它们永远不会返回正面结果.我以前没有在iOS编程中使用正则表达式,所以我不确定我是否正确,但我基本上复制了类描述页面中的代码.

    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);
Run Code Online (Sandbox Code Playgroud)

我的问题是,有更简单的方法吗?如果没有,我的正则表达式是不正确的?最后,如果我必须使用它,运行时间是否昂贵?我不知道用户的平均文件数量是多少.

谢谢.

regex iphone objective-c nsstring ios5

52
推荐指数
3
解决办法
4万
查看次数

标签 统计

ios5 ×1

iphone ×1

nsstring ×1

objective-c ×1

regex ×1