NSFileManager,文件扩展名

发布于 2024-10-02 03:19:59 字数 613 浏览 1 评论 0原文

我正在创建一个 iPhone 应用程序,它使 iPhone 充当随身碟,以便轻松共享文件。

在第一阶段,我在目录中有一些文件(png,pdf,jpg,zip),我让它们以可变数组的形式显示在表格视图中。它显示在 tableView 中,如下所示,

.DS_Store
.localized
gazelle.pdf
Hamburger_sandwich.jpg
IITD TAJ Picture 028_jpg.jpg
iya_logo_final_b&w.jpg
manifesto09-eng.pdf
RSSReader.sql
SimpleURLConnections.zip
SQLTutorial 

我只想显示文件名,不想显示扩展名。我知道可以在 NSFileManager 中提取文件的扩展名。但我不知道怎么办。请帮助我使我的表格视图看起来像这样

.DS_Store
.localized
gazelle
Hamburger_sandwich
IITD TAJ Picture 028_jpg
iya_logo_final_b&w
manifesto09-eng
RSSReader
SimpleURLConnections
SQLTutorial 

I am creating an iPhone application which makes a iphone act as a pendrive for easy file sharing.

In the first stage, i have some files(png, pdf, jpg, zip) in a directory and i made them display in the tableview in the form of mutable array. It displays in the tableView as shown below,

.DS_Store
.localized
gazelle.pdf
Hamburger_sandwich.jpg
IITD TAJ Picture 028_jpg.jpg
iya_logo_final_b&w.jpg
manifesto09-eng.pdf
RSSReader.sql
SimpleURLConnections.zip
SQLTutorial 

I just want to display the name of the files and i do not want to display the extensions. I know that it is possible to extract the extensions of a file in NSFileManager. But i do not know how. Please help me to make my table view look like this

.DS_Store
.localized
gazelle
Hamburger_sandwich
IITD TAJ Picture 028_jpg
iya_logo_final_b&w
manifesto09-eng
RSSReader
SimpleURLConnections
SQLTutorial 

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

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

发布评论

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

评论(1

情愿 2024-10-09 03:19:59

查看 NSString 的文档,您会发现 stringByDeletingPathExtension

基本思想是,您不想更新现有数组中的对象,而是创建一个文件名不带扩展名的新数组,并丢弃原始数组。

这是可以做到的:

NSArray *stuff = [NSArray arrayWithObjects:@"image1.jpg", @"image2.png", @"image3.tiff", nil];
NSLog(@"input %@", stuff);

stuff = [stuff valueForKey:@"stringByDeletingPathExtension"];
NSLog(@"output %@", stuff);

我不希望您理解该代码,因此这里有一个更简单的版本:

NSArray *stuff = [NSArray arrayWithObjects:@"image1.jpg", @"image2.png", @"image3.tiff", nil];
NSLog(@"input %@", stuff);

NSMutableArray *output = [NSMutableArray array];
for (NSString *filename in stuff) {
    NSString *filenameWithoutExtension = [filename stringByDeletingPathExtension];
    [output addObject:filenameWithoutExtension];
}
NSLog(@"output %@", output);

Look into NSString's documentation, there you'll find stringByDeletingPathExtension.

The basic idea is that you do not want to update objects in existing array, but create a new array with filenames without extension, and throw the original array away.

This is how it can be done:

NSArray *stuff = [NSArray arrayWithObjects:@"image1.jpg", @"image2.png", @"image3.tiff", nil];
NSLog(@"input %@", stuff);

stuff = [stuff valueForKey:@"stringByDeletingPathExtension"];
NSLog(@"output %@", stuff);

I do not expect you to understand that code, therefore here's a more straightforward version:

NSArray *stuff = [NSArray arrayWithObjects:@"image1.jpg", @"image2.png", @"image3.tiff", nil];
NSLog(@"input %@", stuff);

NSMutableArray *output = [NSMutableArray array];
for (NSString *filename in stuff) {
    NSString *filenameWithoutExtension = [filename stringByDeletingPathExtension];
    [output addObject:filenameWithoutExtension];
}
NSLog(@"output %@", output);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文