开发者

NSURLRequest returning null

开发者 https://www.devze.com 2023-03-21 05:51 出处:网络
Someone knows why I\'m getting null in this code? NSURLRequest *requisicao = [NSURLRequest requestWithURL:

Someone knows why I'm getting null in this code?

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

When I debug this line and preview the content of this variable, I receive null -

"<NSURLRequest (null)>"

My URLAd开发者_开发百科dress is a request for a JSON service.


It's most likely null because URLAddress contains characters that need to be percent-escaped (which is what the URLWithString method needs).

Try using stringByAddingPercentEscapesUsingEncoding:

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

However, see this answer by Dave DeLong which points out limitations of stringByAddingPercentEscapesUsingEncoding and alternatives to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消