gpt4 book ai didi

ios - 关闭常见任务的最佳方法是什么

转载 作者:行者123 更新时间:2023-11-28 15:54:04 27 4
gpt4 key购买 nike

不要因为庞大的代码片段而 panic ,您只需要了解在我的代码中我有多个具有相同重复代码的函数。我想做一个通用的任务闭包,但每个任务闭包至少有几行(这里是 2 行)特定于每个任务闭包。

我有 Corona 背景,我曾经在其中为所有与服务器相关的代码(只是所有 HTTP 调用)保留一个公共(public)文件(您可以将其视为一个类)。哪些是最好的,因此可以将多个常见的东西概括为多个函数使用。

如果我关闭一个通用任务,那么我将如何将这些数据传递给特定的 VC(其名称作为参数传递给特定的函数)

请让我了解 iOS 开发人员如何实现它。有哪些最佳实践?你可以只看到 1-2 个函数不需要理解我的整个代码。

请不要推荐 Alamofire,我不想依赖第 3 方库。

 import Foundation



class Server
{
static let baseURL="http://www.bewrapd.com/api/call/"

class func convertDataToArray(_ data: Data) -> NSArray
{

do
{

let json = try JSONSerialization.jsonObject(with: data , options: [])

print(json)

return json as! [[String:AnyObject]] as NSArray

}


catch let error as NSError
{
print(error)
}

return []
}

class func convertStringToDictionary(_ data: Data) -> [String:AnyObject]?
{

do
{

let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject]

return json
}
catch let error as NSError
{
print(error)
}

return nil
}

//-----------------------------------------

class func tryToLogin(target: LoginViewController, userName: String, password: String )
{

var request = URLRequest(url: URL(string: baseURL+"checkLoginForIos")!)
request.httpMethod = "POST"
let postString = "userName="+userName+"&password="+password
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")

let finalData = Server.convertStringToDictionary(data)
target.nextAction(finalData!)

}

task.resume()

}

class func fetchCarsForUser(target: CarSelectionViewController)
{

var request = URLRequest(url: URL(string: baseURL+"getAddedCarsByUser")!)
request.httpMethod = "POST"
let postString = "userId=\(userId!)"
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")

let finalData = Server.convertDataToArray(data)
target.nextAction(finalData as! [[String : AnyObject]])


}
task.resume()


}

class func updateCarsStatusForUser(target: CarSelectionViewController, carId: Int, status: Bool)
{

var request = URLRequest(url: URL(string: baseURL+"updateCarBookingStatus")!)
request.httpMethod = "POST"
let postString = "carId=\(carId)&status=\(status)"
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")


let finalData = Server.convertStringToDictionary(data)
target.carStatusChangeCallback(finalData!)


}

task.resume()

}


class func unbookCar(target: MenuController, status: Bool)
{

var request = URLRequest(url: URL(string: baseURL+"updateCarBookingStatus")!)
request.httpMethod = "POST"
let postString = "carId=\(carId!)&status=\(status)"
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")


let finalData = Server.convertStringToDictionary(data)
target.nextAction(finalData!)


}

task.resume()

}

//-----------------------------------------

class func fetchCurrentCampaign(target: CampaignViewController )
{

var request = URLRequest(url: URL(string: baseURL+"getCurrentCampaign")!)
request.httpMethod = "POST"
let postString = "userId=\(userId!)&carId=\(carId!)"
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)

//--------------

let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)!
print("responseString = \(responseString)")

let finalData = Server.convertStringToDictionary(data)
target.nextAction(finalData!)

}

task.resume()


}

class func fetchCarHistory(target: HistoryTableViewController )
{

var request = URLRequest(url: URL(string: baseURL+"campaignHistory")!)
request.httpMethod = "POST"
let postString = "userId=\(userId!)&carId=\(carId!)"
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")

let finalData = Server.convertDataToArray(data)
target.nextAction(finalData as! [[String : AnyObject]] as NSArray)


}
task.resume()
}

class func fetchTripsForCampaign(target: TripsViewController, jobId: Int )
{

var request = URLRequest(url: URL(string: baseURL+"gpsHistory")!)
request.httpMethod = "POST"
let postString = "jobAppId=\(jobId)"
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")

let finalData = Server.convertDataToArray(data)
target.nextAction(finalData as! [[String : AnyObject]] as NSArray)


}
task.resume()
}

class func fetchTripsGeoCoordinates(target: RoutesMapViewController, tripId: Int )
{

//------
var request = URLRequest(url: URL(string: baseURL+"mapHistory")!)
request.httpMethod = "POST"
let postString = "tripId=\(tripId)"
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")

let finalData = Server.convertDataToArray(data)
target.nextAction(finalData as! [[String : AnyObject]] as NSArray)


}
task.resume()
}

class func fetchCampaignList(target: CampaignListViewController )
{

var request = URLRequest(url: URL(string: baseURL+"getCampaignList")!)
request.httpMethod = "POST"
let postString = "userId=\(userId!)&carId=\(carId!)"//+String(describing: userId)
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
{
// check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")

let finalData = Server.convertDataToArray(data)
target.nextAction(finalData as! [[String : AnyObject]] as NSArray)


}
task.resume()
}

class func applyForCampaign(target: JoApplyViewController, campaignId: Int )
{

var request = URLRequest(url: URL(string: baseURL+"applyCampaign")!)
request.httpMethod = "POST"
let postString = "campaignId=\(campaignId)&userId=\(userId!)&carId=\(carId!)"

print("### \(postString)")

request.httpBody = postString.data(using: .utf8)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")


let finalData = Server.convertStringToDictionary(data)
target.nextAction(finalData!)

}

task.resume()


}

class func sendTripData(target: MapViewController, tripDataJSONStr: String)
{

var request = URLRequest(url: URL(string: baseURL+"sendTripCordinatesForIos")!)
request.httpMethod = "POST"

let postString = "request=" + tripDataJSONStr

print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")


let finalData = Server.convertStringToDictionary(data)
target.nextAction(finalData!)

}

task.resume()


}


}

最佳答案

您可以像下面这样使用闭包,不确定您是否需要任何其他要求,实际上与您当前的代码没有区别,只是更容易使用:

class func tryToLogin(userName: String, password: String, completion: (_ result: [String:Any])->() )
{

var request = URLRequest(url: URL(string: baseURL+"checkLoginForIos")!)
request.httpMethod = "POST"
let postString = "userName="+userName+"&password="+password
print("### \(postString)")

request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")

let finalData = Server.convertStringToDictionary(data)
completion(finalData)

}

task.resume()

}

在您的 LoginViewController 上,只需正常调用它,命名数据字典并在关闭时调用 nextAction(finalData!)

关于ios - 关闭常见任务的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41996711/

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