I want to parse the location of my Documents Directory to NSUrl from NSString I have.

when my console NSLog(stringURL)

i get: "/var/mobile/Applications/2B35D06D-6E4A-40F2-833E-28463A946834/Library/Application Support/MyAppDirectory" as an output.

but when I do

NSURL* url = [NSURL URLWithString:stringUrl];

and NSLog(url) i get (null) as an output.

WHat am I doing wrong here?

有帮助吗?

解决方案

From the apple documentation "An NSURL object initialized with URLString. If the URL string was malformed or nil, returns nil." Your stringURL isn't a correct formed url. For reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString:

What you actually want to use is: fileURLWithPath: isDirectory: instead.

NSURL *url = [NSURL fileURLWithString:[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] isDirectory:YES];
NSLog(@"%@", url);

其他提示

How about:

NSURL* url = [NSURL fileURLWithString:stringUrl];
//                  ^^^^

Use fileURLWithPath instead (since you are passing a file path):

NSURL* url = [NSURL fileURLWithPath:stringUrl];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top