获取NSString中包含的文件的扩展名 [英] Get the extension of a file contained in an NSString

查看:401
本文介绍了获取NSString中包含的文件的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSMutable字典,它以fileone.doc或filetwo.pdf的简单形式包含文件ID及其文件名+扩展名。我需要确定在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编程中使用正则表达式,所以我不完全确定我是否正确,但我基本上复制了类描述页面中的代码。

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中包含的文件的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆