The*_*Jer 52 regex iphone objective-c nsstring ios5
我有一个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)
我的问题是,有更简单的方法吗?如果没有,我的正则表达式是不正确的?最后,如果我必须使用它,运行时间是否昂贵?我不知道用户的平均文件数量是多少.
谢谢.
Dav*_*rry 150
您正在寻找 [fileType pathExtension]
//NSURL *url = [NSURL URLWithString: fileType];
NSLog(@"extension: %@", [fileType pathExtension]);
Run Code Online (Sandbox Code Playgroud)
编辑你可以在NSString上使用pathExtension
感谢David Barry
小智 5
尝试这个 :
NSString *fileName = @"resume.doc";
NSString *ext = [fileName pathExtension];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38266 次 |
| 最近记录: |