我已经使用swift和Objective-C来使用其参数调用API,我已经在Objective C中成功完成,但是我在Swift中遇到了使用其参数调用API的问题。

我在两个参数中使用的是相同的键,即“密码”。它在Swift中不起作用,但是在 objective-c 中却是正确的。

请指导我并更新我的Swift代码。

谢谢

Swift代码

var params: [String: String] = ["FirstName":name, "ContactNumber":contact, "Email":email, "password": password,"password":repassword,"Rd_UserType":type]


    var request = NSMutableURLRequest(URL: NSURL(string: "http://local.in/api/AccountApi")!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        println("Response: \(response)")

错误
fatal error: dictionary literal contains duplicate keys

objective-c 代码工作正常。

目标代码C
        NSString *post =[[NSString alloc] initWithFormat:@"FirstName=%@&ContactNumber=%@&Email=%@&password=%@&password=%@&Rd_UserType=%@",Name.text,ContactNumber.text,EmailAddress.text,Password.text,ReTypePassword.text,type];


        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"http://local.in/api/AccountApi"];



        NSLog(@"%@",url);


        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"Response code: %ld", (long)[response statusCode]);

        if ([response statusCode] >= 200 && [response statusCode] < 300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);
        }

最佳答案

您的Swift代码从根本上不同于Obj-C代码。斯威夫特等效

NSString *post =[[NSString alloc] initWithFormat:@"FirstName=%@&ContactNumber=%@&Email=%@&password=%@&password=%@&Rd_UserType=%@",Name.text,ContactNumber.text,EmailAddress.text,Password.text,ReTypePassword.text,type];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

会像
let post = "FirstName=\(Name.text)&ContactNumber=\(ContactNumber.text)&..."
let postData = post.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!
let postLength = "\(postData.length)"

您正在创建一个JSON字典作为发布数据。失败是因为
字典的同一个键不能有两个值。另外JSON可能不是
服务器期望什么。

10-08 06:11
查看更多