【发布时间】:2016-04-12 00:54:54
【问题描述】:
借助 Alamofire 和 Swiftyjson,我可以通过下面的代码向我的服务器发起 HTTP 请求并检索 JSON 对象。
但我无法将来自 Swiftyjson 的自定义类 JSON 作为我的委托方法的参数传递。
我应该怎么做才能修复这个错误?
有错误的代码行:
optional func didReceiveUserInfo(userInfo: JSON) //This the line I get the error
错误: 方法不能是 @objc 协议的成员,因为在 Objective-C 中无法表示参数的类型
这是我正在使用的完整代码:
import UIKit
import Alamofire
import SwiftyJSON
@objc protocol UserModelDelegate {
optional func didReceiveUserInfo(userInfo: JSON) //This is the line I get the error
}
class UserModel: NSObject, NSURLSessionDelegate {
// Instantiate the delegate
var delegate: UserModelDelegate?
override init() {
super.init()
}
func getUserInfo(token: String) {
let url = "http://test.app/api/userInfo"
let headers = [
"Authorization":"Bearer \(token)",
"Content-Type": "application/x-www-form-urlencoded"
]
Alamofire.request(.GET, url, headers: headers).responseJSON { response in
switch response.result {
case .Success(let data):
let json = JSON(data)
self.delegate?.didReceiveUserInfo?(json) //This is where I pass the JSON custom class type as an argument
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}
}
【问题讨论】:
标签: ios json swift swifty-json