来自NSString的NSUrl返回null [英] NSUrl from NSString returns null
问题描述
我想从 NSString
解析我的文档目录的位置到 NSUrl
。
I want to parse the location of my Documents Directory to NSUrl
from NSString
I have.
当我的控制台NSLog(stringURL)
when my console NSLog(stringURL)
i get: "/var/mobile/Applications/2B35D06D-6E4A-40F2-833E-28463A946834/Library/Application Support/MyAppDirectory" as an output.
但是当我这样做时
NSURL* url = [NSURL URLWithString:stringUrl];
和NSLog(url)我得到(null)作为输出。
and NSLog(url) i get (null) as an output.
我在这里做错了什么?
推荐答案
从苹果文档初始化的NSURL对象URLString。如果URL字符串格式错误或为nil,则返回nil。您的stringURL不是正确形成的网址。供参考: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/ URLWithString :
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:
您实际想要使用的是: fileURLWithPath:isDirectory:
而不是。
What you actually want to use is: fileURLWithPath: isDirectory:
instead.
NSURL *url = [NSURL fileURLWithString:[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] isDirectory:YES];
NSLog(@"%@", url);
这篇关于来自NSString的NSUrl返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!