从文件系统或URL解析Objective-C中的m3u文件 [英] Parsing of m3u files in Objective-C for iPhone from file system or URL

查看:195
本文介绍了从文件系统或URL解析Objective-C中的m3u文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的帮助。我对Objective-C有一些麻烦。

Thanks for your help. I had some trouble with Objective-C.

我在编程中有点新鲜。 Xcode 4.0.2。

I'm some kinde of NEWbe in programing. Xcode 4.0.2.

所以我有一些代码。此代码应该从m3u播放列表中的链接,并将其添加到anArray。 (所以我会得到 NSArray(NSMutableArray)中的某些链接)

So I had some code. This code should take a link from m3u playlist and add it to anArray. (So I will get the NSArray(NSMutableArray) with certain links in it)

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

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

All the time I had (null) in NSLog Message. All the time when I try NSLog or if/else statement to check is there is link in array it gives me the null object in it.

我认为问题是在m3u类型,我试图改变类型在txt和阅读。 (对于那些不知道,M3U只是UTF-8编码的文本,更改类型应该给出结果)
然后我试过.txt文件,但它不工作。

After that I thought the problem was in m3u type and I've tried to change type in txt and read. (For those who don't know, M3U is just the text in UTF-8 encoding and the changing type should give the result) Then I've tried the .txt files but it doesn't work too. So there is the code of it.

//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)。
所有的东西,没有任何希望到达我的目的地之后,我试图读取txt文件逐行分隔/ n与5个链接。

Checking is there is m3u file worked fine. I had Addres to Renamed file too. But when it was checking is there is renamed file, there was no file (null in NSLog). After all that stuff, and without any hope to reach my destination I've tried to read txt file line by line separated by /n with 5 links in it.

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的尝试的几次,并有Nothing但为空)

Both Messages were NULL One more thing all this stuff I tried to make in -(IBAction)fileRead { } linked to button (Yes I've presed button every time to check my NSLog)Program was checked in iPhone Simulator. Will be glad if someone say what is the trouble. Also if there is easier way to make this with url. (Tried Couple times with NSUrl and had Nothing but null )

推荐答案

只是因为你改变了路径并不意味着你已经重命名/移动/复制了一个项目,路径只是一个字符串。使用 NSFileManager 方法,例如

Just because you've changed the path doesn't mean that you've renamed/moved/copied an item, path is just a string. Use NSFileManager methods like

- moveItemAtURL:toURL:error:

- moveItemAtPath:toPath:error:

此外,NSString不关心扩展,所以它是完全安全的读取您的m3u文件到NSString,不需要重命名。

Also, NSString doesn't care about extension, so it's completely safe to read your m3u file to NSString, no need to rename it.

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

这篇关于从文件系统或URL解析Objective-C中的m3u文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆