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

发布于 2025-01-04 12:28:07 字数 1113 浏览 3 评论 0原文

我有一个 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 编程中使用过正则表达式,所以我不完全确定我做得是否正确,但我基本上是从类描述页面复制了代码。

    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);

我的问题是,有没有更简单的方法来做到这一点?如果不是,我的正则表达式不正确吗?最后,如果我必须使用它,运行时成本会很高吗?我不知道用户平均拥有的文件量是多少。

谢谢。

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

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.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

纸短情长 2025-01-11 12:28:07

您正在寻找 [fileType pathExtension]

NSString 文档:pathExtension

You're looking for [fileType pathExtension]

NSString Documentation: pathExtension

栩栩如生 2025-01-11 12:28:07
//NSURL *url = [NSURL URLWithString: fileType];
NSLog(@"extension: %@", [fileType pathExtension]);

编辑你可以在 NSString 上使用 pathExtension

感谢 David Barry

//NSURL *url = [NSURL URLWithString: fileType];
NSLog(@"extension: %@", [fileType pathExtension]);

Edit you can use pathExtension on NSString

Thanks to David Barry

安稳善良 2025-01-11 12:28:07

试试这个:

NSString *fileName = @"resume.doc";  
NSString *ext = [fileName pathExtension];

Try this :

NSString *fileName = @"resume.doc";  
NSString *ext = [fileName pathExtension];
沒落の蓅哖 2025-01-11 12:28:07

试试这个,它对我有用。

NSString *fileName = @"yourFileName.pdf";
NSString *ext = [fileName pathExtension];

此处的文档为 NSString pathExtension

Try this, it works for me.

NSString *fileName = @"yourFileName.pdf";
NSString *ext = [fileName pathExtension];

Documentation here for NSString pathExtension

夕嗳→ 2025-01-11 12:28:07

尝试使用 [fileType pathExtension] 获取文件的扩展名。

Try using [fileType pathExtension] to get the extension of the file.

一念一轮回 2025-01-11 12:28:07

在 Swift 3 中你可以使用扩展:

extension String {

public func getExtension() -> String? {

        let ext = (self as NSString).pathExtension

        if ext.isEmpty {
            return nil
        }

        return ext
    }
}

In Swift 3 you could use an extension:

extension String {

public func getExtension() -> String? {

        let ext = (self as NSString).pathExtension

        if ext.isEmpty {
            return nil
        }

        return ext
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文