Parsing of m3u files in Objective-C for iPhone fro

2020-06-22 10:11发布

问题:

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

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

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

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.

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

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

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 )

回答1:

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: or

– moveItemAtPath:toPath:error:.

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