如何设置 success/failure 块以便我可以使用 Swift 将字符串传递到其中?
How do I set up a success/failure block so that I can pass a string into it using Swift?
我有一个 API 方法,可以 return 为特定联赛设置球队。我需要做的就是传入一些参数。
例如,API URL 看起来像这样:http://api.website.com/api/teams?season=last&league=0Xc334xUK4
这是我的代码:
@objc class APITeam: NSObject {
var leagueObjectID: NSString!
let baseUrl = "http://api.website.com/api"
static let sharedInstance = APITeam()
static let getTeamsEndpoint = "/teams"
static let params = "?season=last&league="
private override init() {}
func getTeams (_ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void) {
var request = URLRequest(url: URL(string: baseUrl + APITeam.getTeamsEndpoint + APITeam.params)!)
let session = URLSession.shared
request.httpMethod = "GET"
let task = session.dataTask(with: request) { (data : Data?, response : URLResponse?, error : Error?) in
if(error != nil){
onFailure(error!)
} else{
do {
let result = try JSONSerialization.jsonObject(with: data!)
onSuccess(result)
} catch {
print(error)
}
}
}
task.resume()
}
}
为了让这个API方法能够return数据,我需要在发出请求时传入我想要球队的联赛的objectID。
我尝试将参数添加到:
func getTeams (_ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void, leagueObjectID: String) {
var request = URLRequest(url: URL(string: baseUrl + APITeam.getTeamsEndpoint + APITeam.params + leagueObjectID)!)
这没有按预期工作。当我在另一个 class 中使用该方法时,"leagueObjectID" 作为成功的附加部分,如果有意义的话,错误块。我需要能够将 leagueObjectID 传递到方法中,以便在 URL 请求的末尾使用它。
这就是我在 objective-c class 中称呼 API 团队的方式:
[[APITeam sharedInstance] getTeams:^(id result) {
} onFailure:^(NSError * error) {
}];
如您所见,我添加到 getTeams 函数的额外参数没有显示,当我尝试手动添加时出现错误。
你会如何处理?
一个例子将不胜感激。
感谢您的宝贵时间
static
可能无法像您在其他语言中习惯的那样工作。 static var
表现得像 class var
.
如果我没理解错的话,这会满足你的需求
@objc class APITeam: NSObject {
private static let baseUrl = "http://api.website.com/api"
private static let getTeamsEndpoint = "/teams"
let params = "?season=last&league="
private override init() {}
func getTeam(id: String, _ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void) {
var request = URLRequest(url: URL(string: APITeam.baseUrl + APITeam.getTeamsEndpoint + APITeam.params + id)!)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
onFailure(error)
} else if let responseData = data {
do {
let result = try JSONSerialization.jsonObject(with: responseData)
onSuccess(result)
} catch {
print(error)
}
}
}.resume()
}
}
用法:
APITeam().getTeam(id: "0Xc334xUK4", ..your completion handlers here..)
改为
func getTeams (leagueObjectID: String, onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void)
这对我有用:
@objc class APITeam: NSObject {
var leagueObjectID: NSString!
let baseUrl = "http://api.website.com/api"
static let sharedInstance = APITeam()
static let getTeamsEndpoint = "/teams"
static let params = "?season=last&league="
private override init() {}
func getTeams (leagueObjectID: String, _ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void) {
var request = URLRequest(url: URL(string: baseUrl + APITeam.getTeamsEndpoint + APITeam.params + leagueObjectID)!)
let session = URLSession.shared
request.httpMethod = "GET"
let task = session.dataTask(with: request) { (data : Data?, response : URLResponse?, error : Error?) in
if(error != nil){
onFailure(error!)
} else{
do {
let result = try JSONSerialization.jsonObject(with: data!)
onSuccess(result)
} catch {
print(error)
}
}
}
task.resume()
}
}
然后在我的Objective-Cclass:
[[APITeam sharedInstance] getTeamsWithLeagueObjectID:@"93PwHe5e4S" :^(id league) {
NSLog(@"THE LEAGUE: %@", league);
} onFailure:^(NSError * error) {
}];
在尝试访问原始 class 之前构建应用程序也很重要。这对我来说是至关重要的一步。有时我不需要这样做,但是这一次,在构建应用程序之前我无法访问新更改的 APITeam getTeams 函数。
我有一个 API 方法,可以 return 为特定联赛设置球队。我需要做的就是传入一些参数。
例如,API URL 看起来像这样:http://api.website.com/api/teams?season=last&league=0Xc334xUK4
这是我的代码:
@objc class APITeam: NSObject {
var leagueObjectID: NSString!
let baseUrl = "http://api.website.com/api"
static let sharedInstance = APITeam()
static let getTeamsEndpoint = "/teams"
static let params = "?season=last&league="
private override init() {}
func getTeams (_ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void) {
var request = URLRequest(url: URL(string: baseUrl + APITeam.getTeamsEndpoint + APITeam.params)!)
let session = URLSession.shared
request.httpMethod = "GET"
let task = session.dataTask(with: request) { (data : Data?, response : URLResponse?, error : Error?) in
if(error != nil){
onFailure(error!)
} else{
do {
let result = try JSONSerialization.jsonObject(with: data!)
onSuccess(result)
} catch {
print(error)
}
}
}
task.resume()
}
}
为了让这个API方法能够return数据,我需要在发出请求时传入我想要球队的联赛的objectID。
我尝试将参数添加到:
func getTeams (_ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void, leagueObjectID: String) {
var request = URLRequest(url: URL(string: baseUrl + APITeam.getTeamsEndpoint + APITeam.params + leagueObjectID)!)
这没有按预期工作。当我在另一个 class 中使用该方法时,"leagueObjectID" 作为成功的附加部分,如果有意义的话,错误块。我需要能够将 leagueObjectID 传递到方法中,以便在 URL 请求的末尾使用它。
这就是我在 objective-c class 中称呼 API 团队的方式:
[[APITeam sharedInstance] getTeams:^(id result) {
} onFailure:^(NSError * error) {
}];
如您所见,我添加到 getTeams 函数的额外参数没有显示,当我尝试手动添加时出现错误。
你会如何处理? 一个例子将不胜感激。
感谢您的宝贵时间
static
可能无法像您在其他语言中习惯的那样工作。 static var
表现得像 class var
.
如果我没理解错的话,这会满足你的需求
@objc class APITeam: NSObject {
private static let baseUrl = "http://api.website.com/api"
private static let getTeamsEndpoint = "/teams"
let params = "?season=last&league="
private override init() {}
func getTeam(id: String, _ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void) {
var request = URLRequest(url: URL(string: APITeam.baseUrl + APITeam.getTeamsEndpoint + APITeam.params + id)!)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
onFailure(error)
} else if let responseData = data {
do {
let result = try JSONSerialization.jsonObject(with: responseData)
onSuccess(result)
} catch {
print(error)
}
}
}.resume()
}
}
用法:
APITeam().getTeam(id: "0Xc334xUK4", ..your completion handlers here..)
改为
func getTeams (leagueObjectID: String, onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void)
这对我有用:
@objc class APITeam: NSObject {
var leagueObjectID: NSString!
let baseUrl = "http://api.website.com/api"
static let sharedInstance = APITeam()
static let getTeamsEndpoint = "/teams"
static let params = "?season=last&league="
private override init() {}
func getTeams (leagueObjectID: String, _ onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void) {
var request = URLRequest(url: URL(string: baseUrl + APITeam.getTeamsEndpoint + APITeam.params + leagueObjectID)!)
let session = URLSession.shared
request.httpMethod = "GET"
let task = session.dataTask(with: request) { (data : Data?, response : URLResponse?, error : Error?) in
if(error != nil){
onFailure(error!)
} else{
do {
let result = try JSONSerialization.jsonObject(with: data!)
onSuccess(result)
} catch {
print(error)
}
}
}
task.resume()
}
}
然后在我的Objective-Cclass:
[[APITeam sharedInstance] getTeamsWithLeagueObjectID:@"93PwHe5e4S" :^(id league) {
NSLog(@"THE LEAGUE: %@", league);
} onFailure:^(NSError * error) {
}];
在尝试访问原始 class 之前构建应用程序也很重要。这对我来说是至关重要的一步。有时我不需要这样做,但是这一次,在构建应用程序之前我无法访问新更改的 APITeam getTeams 函数。