我有一个APIManager
单身人士
import Foundation
import Alamofire
import SwiftyJSON
class APIManager{
let url = "http://myapilink"
static let sharedInstance = APIManager()
var json = JSON()
func getProgramSessions(program: Int, tokenCode: String, onCompletion: (JSON) -> Void){
let parameters = ["id" : id, "tokenCode" : tokenCode] as [String : Any]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default)
.responseJSON { response in
guard let dataValue = response.result.value else { return }
json = JSON(dataValue)
} OnCompetion (json)
}
名为UserObject
import Foundation
import SwiftyJSON
class UserObject {
var user_no = Int()
var user_link = String()
required init(json: JSON) {
self.user_no = json["user_no"].intValue
self.user_link = json["user_link"].stringValue
}
}
我在Alamofire响应中看到可选数据
Optional(<__NSSingleObjectArrayI 0x600000010750>(
{
user_no = 100;
"user_link" = somelink;
}
)
当我尝试调用Singleton来获取API数据,然后将json解析为基于UserObject类的数组时-我得到一个空数组。
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var id = 0;
var items = [UserObject]()
var tokenCode = "sometokencode";
func addDummyData() {
APIManager.sharedInstance.getProgramSessions(id:1, tokenCode: tokenCode, onCompletion: { (json: JSON) in
items.append(ProgrammsClass(json: JSON(json["session_no"].intValue)))
}) }
addDummyData()
}
}