objective-c - 从文件系统或 URL 解析 Objective-C for iPhone 中的 m3u 文件

标签 objective-c cocoa-touch

下面的示例应该从 m3u 播放列表中获取一个链接并将其添加到一个数组中。 (所以我将获得带有特定链接的 NSArray(NSMutableArray))

NSString *fileContents = [NSString stringWithContentsOfFile:@"myfile.m3u" encoding:NSUTF8StringEncoding error:NULL];
NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];
NSLog (@"%@",lines);

我一直在 NSLog Message 中有 (null)。 一直以来,当我尝试 NSLog 或 if/else 语句来检查数组中是否有链接时,它都会给我空对象。

之后我认为问题出在 m3u 类型中,我尝试更改 txt 中的类型并阅读。 (不知道的人,M3U只是UTF-8编码的文本,改变类型应该给出结果) 然后我尝试了 .txt 文件,但它也不起作用。所以有它的代码。

//Check if there is my file
NSString *addPath = [[NSBundle mainBundle]  pathForResource:@"somefile" ofType:@"m3u" ];
if ([fileMgr fileExistsAtPath:addPath] ) {
    NSLog(@"Yes.We see the file");
}
else {
    NSLog(@"Nope there is no file");
}
//Rename
NSString *path1 = addPath;
NSString *theNewFilename = [path1 stringByReplacingOccurrencesOfString:@"m3u" withString:@"txt"];
NSLog(@"Renamed file adress is %@", theNewFilename); 
   
//Check if there is our renamed file(file manager was allocated before) 
NSString *addPath1 = [[NSBundle mainBundle]  pathForResource:@"somefile" ofType:@"txt" ];
if ([fileMgr fileExistsAtPath:addPath1] ) {
    NSLog(@"Yes we had the renamed file");
}
else {
    NSLog(@"No we don't");
}

检查是否有 m3u 文件工作正常。我也有重命名文件的地址。但是当它检查是否有重命名的文件时,没有文件(NSLog 中为 null)。 在所有这些事情之后,没有任何希望到达我的目的地,我尝试逐行读取由/n 分隔的 txt 文件,其中有 5 个链接。

NSString *fileContents1 = [NSString stringWithContentsOfFile:@"myfile.txt" encoding:NSUTF8StringEncoding error:NULL];
NSArray *lines1 = [fileContents1 componentsSeparatedByString:@"\n"];
NSLog (@"%@",fileContents1);
NSLog (@"%@",lines1);

两条消息均为 NULL 我尝试在 -(IBAction)fileRead { } 链接到按钮的所有这些东西中做的另一件事 (是的,我每次都按下按钮来检查我的 NSLog)在 iPhone 模拟器中检查了程序。如果有人说有什么麻烦,我们会很高兴。另外,如果有更简单的方法可以使用 url 进行此操作。 (尝试使用 NSUrl 几次,结果只有 null )

最佳答案

仅仅因为您更改了路径并不意味着您已经重命名/移动/复制了一个项目,路径只是一个字符串。使用像

这样的 NSFileManager 方法

– moveItemAtURL:toURL:error:

– moveItemAtPath:toPath:error:

此外,NSString 不关心扩展名,因此将 m3u 文件读取到 NSString 是完全安全的,无需重命名。

NSString *addPath = [[NSBundle mainBundle]  pathForResource:@"somefile" ofType:@"m3u" ];
if ([fileMgr fileExistsAtPath:addPath] ) {
    NSLog(@"Yes.We see the file");
    NSString *fileContents1 = [NSString stringWithContentsOfFile:addPath encoding:NSUTF8StringEncoding error:NULL];
    NSArray *lines1 = [fileContents1 componentsSeparatedByString:@"\n"];
    NSLog (@"%@",fileContents1);
    NSLog (@"%@",lines1);
}
else {
    NSLog(@"Nope there is no file");
}

关于objective-c - 从文件系统或 URL 解析 Objective-C for iPhone 中的 m3u 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914330/

相关文章:

.net - Objective C 真的编译了吗?它不是更像 Visual Basic/.NET 运行时吗?那么是什么阻止它移植到其他平台呢?

ios - 比较 1 个 NSarray 和 1 个 NSset 在其中的 1 个中重复计数

iphone - 日期格式问题 - 将字符串转换为 MMM d、yyyy 格式的日期

ios - 如何将 UISearchbar 和 Searchdisplaycontroller 与多个 TableView 一起使用?

ios - 日志未出现在 Web Inspector 中

objective-c - 我自己的方法的自定义完成 block

objective-c - 用动画交换 rootViewController

ios - 新手: about ios/objective -c variable definition in a class

iphone - 警告 : Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame

ios - 弹出回 ViewController 时,UITableView 中的单元格保持突出显示