我有一个API方法可以返回特定联赛的球队。我需要做的就是传递一些参数。
例如,API网址如下所示: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方法返回数据,我需要在提出请求时传递我想要团队的联盟的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)!)
这并不像预期的那样有效。当我在另一个类中使用该方法时," leagueObjectID"作为成功的另一部分,如果这是有意义的错误阻止。我需要能够将leagueObjectID传递给方法,以便在请求所在的URL末尾使用它。
这就是我在一个Objective-c类中调用APITeam的方法:
[[APITeam sharedInstance] getTeams:^(id result) {
} onFailure:^(NSError * error) {
}];
正如您所看到的,我添加到getTeams函数的额外参数不会显示,当我尝试手动添加它时,我收到错误。
你会怎么处理这个? 一个例子将非常感激。
感谢您的时间
答案 0 :(得分:1)
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..)
答案 1 :(得分:1)
将其更改为
func getTeams (leagueObjectID: String, onSuccess: @escaping(Any) -> Void, onFailure: @escaping(Error) -> Void)
答案 2 :(得分:0)
这对我有用:
@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-C课程中:
[[APITeam sharedInstance] getTeamsWithLeagueObjectID:@"93PwHe5e4S" :^(id league) {
NSLog(@"THE LEAGUE: %@", league);
} onFailure:^(NSError * error) {
}];
在尝试访问原始类之前构建应用程序也很重要。这对我来说是至关重要的一步。有时候我不需要这样做,但这一次,在我构建应用程序之前,我无法访问新更改的APITeam getTeams功能。