gpt4 book ai didi

objective-c - 将 Objective-C POST 请求转换为 Swift

转载 作者:行者123 更新时间:2023-11-28 12:51:50 25 4
gpt4 key购买 nike

我是 Swift 的新手,不了解 Objective C。我想将此函数完全转换为 Swift。

void post_req()

{
NSString *oper = @"99";
NSString *key = @"22";
NSString *query = @"22";
NSString *event_id = @"2";
NSError * error = nil;

NSString *myRequestString = [[NSString alloc] initWithFormat:@"operation=%@&key=%@&payload=%@", oper, key, query, event_id];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"numbers"]];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody: myRequestData];
NSURLResponse *response;
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSError *e = nil;
NSData *jData = [content dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jData options: NSJSONReadingMutableContainers error: &e];

id<NSObject> value = JSON[@"response"];

NSLog(@"responseData: %@", JSON);
NSLog(@"payData: %@", value);
NSString* responseString = [[NSString alloc] initWithData:returnData encoding:NSNonLossyASCIIStringEncoding];

if ([content isEqualToString:responseString])
{
NSLog(@"responseData: %@", content);
}

这就是我目前所拥有的。它运行不正常。

func post_req() {

let oper = "99"
let key = "22"
let query = "22"
let event_id = "10"
var error : NSError?

let urlString = "http://numbers"
let url = NSURL(string: urlString)!
let request = NSMutableURLRequest(URL: url)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"

request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

var myRequestString = NSString(format: "operation=%@&key=%@&payload=%@", oper, key, query, event_id)
var myRequestData = NSData(bytes: myRequestString.UTF8String, length: myRequestString.length)
//request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"content-type")
request.HTTPBody = myRequestData

let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

var content = String(data: data!, encoding: NSUTF8StringEncoding)!

var jData = content.dataUsingEncoding(NSUTF8StringEncoding)

do {

let JSON = try NSJSONSerialization.JSONObjectWithData(jData!, options: .MutableContainers)


print("responseData: %@", JSON)

} catch {
print("error")
}
print("responseData: %@", content)

})
task.resume()
}

我认为问题出现在请求的末尾,与编码有关,但我只想包含所有详细信息。

最佳答案

你为什么不使用 Alamofire。它默认处理 jSon。我们不必关心它。只需将库集成到您的项目中即可。这是这样做的说明:github.com/Alamofire/Alamofire

使用“import Alamofire”将库导入您的类(class)。然后使用以下代码:

let parameters = ["oper": "99" ,"key" : "22" , "query" : "22" , "event_id" : "2" , "error" : "" ]

Alamofire.request(.POST, "http://yoururl.com", parameters: parameters)
.responseString { response in
print("Response String: \(response.result.value)")
}
.responseJSON { response in
print("Response JSON: \(response.result.value)")
//Handle the json response here
if let parseJSON = response.result.value {

}
}

关于objective-c - 将 Objective-C POST 请求转换为 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36306389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com