NSUrl from NSString returns null

拈花ヽ惹草 提交于 2019-11-29 17:25:40

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!