swift中的URL编码(发布用户名和密码)

时间:2014-06-08 06:27:43

标签: swift

我的代码如下所示,写在目标C中。

        NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.txtUsername text],[self.txtPassword text]];
           NSLog(@"PostData: %@",post);
           NSURL *Url=[NSURL URLWithString:getLoginURL];
          NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
           NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
           NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

我想在swift中编写这段代码。

任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:4)

let post = "username=\(self.txtUsername.text)&password=\(self.txtPassword.text)"
println("PostData: \(post)")
let url = NSURL(getLoginURL)
let postData = post.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
let postLength = "\(postData.length)"
let request = NSMutableURLRequest()