将字符串转换为 url
Convert string into url
当我将字符串转换为 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
可能您正在将文本字段分配给 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];
试试这个
let NSHipster = URL(string: "http://nshipster.com/") //returns 一个有效的 URL
让无效URL = URL(string: "www.example.com/This is a sentence") //Returns nil
更多详情:
http://www.codingexplorer.com/creating-and-modifying-nsurl-in-swift/
当我将字符串转换为 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
可能您正在将文本字段分配给 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];
试试这个
let NSHipster = URL(string: "http://nshipster.com/") //returns 一个有效的 URL
让无效URL = URL(string: "www.example.com/This is a sentence") //Returns nil
更多详情: http://www.codingexplorer.com/creating-and-modifying-nsurl-in-swift/