With the code below, I'm able to launch an HTTP request to my server and retrieve a JSON object thanks to Alamofire and Swiftyjson.
But I'm not able to pass the custom class JSON from Swiftyjson as an argument of my delegate method.
What should I do to fix this error?
Code line with error:
optional func didReceiveUserInfo(userInfo: JSON) //This the line I get the error
Error: Method cannot be a member of an @objc protocol because the type of parameter cannot be represented in in Objective-C
Here is the full code I'm using:
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)")
}
}
}
}