下面的示例应从 m3u 播放列表获取链接并将其添加到 anArray。 (所以我会得到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 中为空)。
毕竟,在没有任何希望到达目的地的情况下,我尝试逐行读取由 /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 之外什么也没有)