将字符串转换为url

时间:2017-09-27 06:41:41

标签: ios objective-c json nsurl

当我将字符串转换为NSURL时,我得到了一些东西。 我的代码ID ...

NSString *urlString = [[NSString alloc]initWithFormat:@"http://host name/index.php?id=%@&mob=%@&name=%@&mail=%@&m=23", self.deviceId, self.pnumber, self.name, self.email];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *urlPattern = [NSURL URLWithString:urlString];
NSLog(@"%@", urlPattern);

http://host name/index.php?dev_id=73EA1D1D-E6E3-485C-883D-DF952E116&mob=%3CUITextField:%200x7fe247063c00;%20frame%20=%20(0%20128.333;%20310.667%2040.6667);%20text%20=%20'';%20opaque%20=%20NO;%20autoresize%20=%20RM+BM;%20gestureRecognizers%20=%20%3CNSArray:%200x608000244c50%3E;%20layer%20=%20%3CCALayer:%200x60800003eee0%3E%3E&name=%3CUITextField:%200x7fe247055800;%20frame%20=%20(0%2044;%20310.667%2040.3333);%20text%20=%20'';%20opaque%20=%20NO;%20autoresize%20=%20RM+BM;%20gestureRecognizers%20=%20%3CNSArray:%200x608000244410%3E;%20layer%20=%20%3CCALayer:%200x60800003e240%3E%3E&mail=%3CUITextField:%200x7fe24601ae00;%20frame%20=%20(0%20213;%20310.667%2040.3333);%20text%20=%20'';%20opaque%20=%20NO;%20autoresize%20=%20RM+BM;%20gestureRecognizers%20=%20%3CNSArray:%200x608000243c90%3E;%20layer%20=%20%3CCALayer:%200x60c000038580%3E%3E&m=23

如何将字符串转换为NSURL

2 个答案:

答案 0 :(得分:5)

您可能正在将文本字段分配给urlString。检查self.pnumber是文本字段还是字符串。如果textField然后设置为self.pnumber.text并类似地检查所有数据。

self.pnumber, self.name, self.email更改为self.pnumber.text, self.name.text, self.email.text

使用

NSString *urlString = [[NSString alloc]initWithFormat:@"http://host name/index.php?id=%@&mob=%@&name=%@&mail=%@&m=23", self.deviceId, self.pnumber.text, self.name.text, self.email.text];

答案 1 :(得分:0)

试试这个

让NSHipster = URL(字符串:" http://nshipster.com/")//返回有效的网址

让invalidURL = URL(字符串:" www.example.com/这是一个句子")//返回nil

了解更多细节: http://www.codingexplorer.com/creating-and-modifying-nsurl-in-swift/