【问题标题】:Parsing of m3u files in Objective-C for iPhone from file system or URL从文件系统或 URL 解析 iPhone 的 Objective-C 中的 m3u 文件
【发布时间】:2012-06-10 11:24:51
【问题描述】:

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

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

我一直 (null) 在 NSLog 消息中。 一直以来,当我尝试使用 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 中为空)。 毕竟这些东西,并且没有任何希望到达我的目的地,我试图逐行读取 txt 文件,以 /n 分隔,其中包含 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 )

【问题讨论】:

    标签: objective-c cocoa-touch


    【解决方案1】:

    仅仅因为您更改了路径并不意味着您已经重命名/移动/复制了一个项目,路径只是一个字符串。使用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");
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多