【问题标题】:NSURLRequest returning nullNSURLRequest 返回 null
【发布时间】:2011-10-07 17:18:25
【问题描述】:

有人知道我为什么在这段代码中得到空值吗?

NSURLRequest *requisicao = [NSURLRequest requestWithURL:
                               [NSURL URLWithString:URLAddress]];

当我调试此行并预览此变量的内容时,我收到 null -
"<NSURLRequest (null)>"

我的 URLAddress 是对 JSON 服务的请求。

【问题讨论】:

    标签: objective-c nsurl nsurlrequest


    【解决方案1】:

    它很可能为 null,因为 URLAddress 包含需要百分比转义的字符(这是 URLWithString 方法所需要的)。

    尝试使用stringByAddingPercentEscapesUsingEncoding

    NSURLRequest *requisicao = [NSURLRequest requestWithURL:
        [NSURL URLWithString:
            [URLAddress stringByAddingPercentEscapesUsingEncoding:
                            NSUTF8StringEncoding]]];    
    

    但是,请参阅 this answer by Dave DeLong,其中指出了 stringByAddingPercentEscapesUsingEncoding 的局限性及其替代方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      相关资源
      最近更新 更多