Convert NSString to NSURL
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; /* OR */ NSURL *fileURL = [NSURL fileURLWithPath:filePath];
Convert NSURL to NSString
// The output string will have the file:// prefix NSString *filePath= [fileURL absoluteString]; // The output string will have the file path only NSString *filePath= [fileURL path];
Updated @ 2011-01-10: Thanks for the comments of Brandon and Antal.
Converting a NSURL to a NSString File Path is:
NSString *filePath = [fileURL path];
The code you have that uses -absoluteString results in a string that has the “file://” prefix.
LikeLike
thanks for your reminder. =)
LikeLike
I have just spent half an hour trying to figure out what’s wrong with my code, using the methods above — then I have seen the comment….
ykyuen, maybe you could correct the post, so that others wouldn’t have the same problem in the future.
LikeLike
Oops… i have updated the post. Thanks for your comment.
LikeLike
Minor point – but you’re missing the pointer * in the second example for converting NSString to NSURL.
NSURL fileURL = [NSURL fileURLWithPath:filePath];
should be
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
LikeLike
updated.. thanks. =P
LikeLike