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

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

问题描述

我有一个 NSMutable 字典,其中包含文件 ID 及其文件名+扩展名,格式为 fileone.doc 或 filetwo.pdf.我需要确定在我的 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 编程中使用过正则表达式,所以我不完全确定我是否做得对,但我基本上是从 Class Description 页面复制了代码.

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天全站免登陆