无法从NSString创建NSUrl总是为零 [英] Unable to create NSUrl from NSString always getting nil

查看:110
本文介绍了无法从NSString创建NSUrl总是为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSMutableString *string = [[NSMutableString alloc]initWithString:
@"http%3A%2F%2Fsupport24hour.com%2Fworkplace2%2Fbuttler%2Fimage.php%3Fwidth%3D534%26height%3D256%26image%3Duploads%2Fdeals%2FdealImage%2Fdeal_1383005121_IGA+Logo.JPG"];

[string replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [string length])];

NSURL * url =  [NSURL URLWithString:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


推荐答案

我的一位同事面临同样的问题,请尝试以下代码行。希望您的问题得到解决

one of my colleague faced the same problem , try below line of code . Hope your problem will be resolved

NSMutableString *string = [[NSMutableString alloc]initWithString:
                           @"http%3A%2F%2Fsupport24hour.com%2Fworkplace2%2Fbuttler%2Fimage.php%3Fwidth%3D534%26height%3D256%26image%3Duploads%2Fdeals%2FdealImage%2Fdeal_1383005121_IGA+Logo.JPG"];

string = [[NSMutableString alloc] initWithString:[[string stringByURLDecode] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

NSURL * url =  [NSURL URLWithString:string];

NSLog(@"url = %@",url);

其中stringByURLDecode是用于解码URL的类别方法,它就像这样

Where stringByURLDecode is a category method used to decode URL and it goes like this

- (NSString *)stringByURLDecode {

NSMutableString *tempStr = [NSMutableString stringWithString:self];
[tempStr replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];

return [[NSString stringWithFormat:@"%@",tempStr] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

}

最好的运气(年)

这篇关于无法从NSString创建NSUrl总是为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆