gpt4 book ai didi

ios - NSURL :initWithString fails for string obtained from NSSearchPathForDirectoriesInDomains

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:42 27 4
gpt4 key购买 nike

有人能告诉我为什么这段代码会失败吗?调用 initWithString 后 url 为 nil。正在成功填充变量 documentsDirectory。

NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths lastObject];
NSURL* url = [[NSURL alloc]initWithString:documentsDirectory];

谢谢

最佳答案

来自 the documentation :

Parameters
URLString
The string with which to initialize the NSURL object. This string must conform to URL format as described in RFC 2396. This method parses URLString according to RFCs 1738 and 1808.

这意味着必须正确转义特殊字符(空格变为 %20 等);

简单地做:

NSURL *url = [[NSURL alloc] initFileURLWithPath:documentsDirectory];

关于ios - NSURL :initWithString fails for string obtained from NSSearchPathForDirectoriesInDomains,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650763/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com