ios - 来自 NSString 的 NSUrl 返回 null

标签 ios objective-c parsing nsstring nsurl

我想将我的文档目录的位置从我拥有的 NSString 解析为 NSUrl

当我的控制台 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) 作为输出。

我做错了什么?

最佳答案

来自 Apple 文档“使用 URLString 初始化的 NSURL 对象。如果 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 :

您实际想要使用的是:fileURLWithPath: isDirectory:

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

关于ios - 来自 NSString 的 NSUrl 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424892/

相关文章:

iphone - 灵活的iPhone按钮

objective-c - 使用不同的键但相同类型的值对 NSDictionary 的 NSArray 进行排序的最佳方法是什么

c# - 如何连接 2 个列表

html - Android中解析HTML : no XSLT? 有什么用?

python - 使用 ElementTree 在 python 中解析 xml

iphone - 在 View Controller 之间传递数组?

ios - 使用未声明的类型 "tagCell"错误。创建那种 xib 的 UICollectionViewCell

objective-c - Objective C、UIGestureRecognizer的分配与释放

iphone - 保持UISegmentedControl(以及其他)的响应

ios - 如何在 swift 3 中多次延迟动画对象?