我尝试使用AlamofireObjectMapper
映射以下JSON{
"res": [
{
"electiveClusterId": 25,
"multipleSelectionEnabled": false,
"electiveClusterName": null,
"coursesList": [
{
"courseId": 6,
"courseName": "Accounts",
"selected": false,
"classStarted": false
},
{
"courseId": 19,
"courseName": "Javascript",
"selected": false,
"classStarted": false
}
]
},
{
"electiveClusterId": 26,
"multipleSelectionEnabled": true,
"electiveClusterName": null,
"coursesList": [
{
"courseId": 11,
"courseName": "Algorithms and Logics",
"selected": false,
"classStarted": false
},
{
"courseId": 15,
"courseName": "Android App",
"selected": false,
"classStarted": false
}
]
}
]
}
以下是我各自的对象类:
class ElectivesMainResponse: Mappable {
private var _res: [ElectivesResponse]?
var Res: [ElectivesResponse]? {
return _res
}
required init?(map: Map){
mapping(map: map)
}
func mapping(map: Map) {
_res <- map["res"]
}
}
class ElectivesResponse: Mappable {
private var _electiveClusterId: Int!
private var _multipleSelectionEnabled: Bool!
private var _electiveClusterName: String?
private var _courseList: [Course]!
var ClusterId: Int {
return _electiveClusterId
}
var MultipleSelectionEnabled: Bool {
return _multipleSelectionEnabled
}
var ElectiveClusterName: String? {
if let elective = _electiveClusterName {
return elective
} else {
return "Elective"
}
}
var CourseList: [Course]? {
return _courseList
}
required init?(map: Map){
}
func mapping(map: Map) {
_electiveClusterId <- map["electiveClusterId"]
_multipleSelectionEnabled <- map["multipleSelectionEnabled"]
_electiveClusterName <- map["electiveClusterName"]
_courseList <- map["coursesList"]
}
}
class Course: Mappable {
private var _courseId: Int!
private var _courseName: String!
private var _courseSelected: Bool!
private var _classStarted: Bool!
var CourseId: Int {
return _courseId
}
var CourseName: String {
return _courseName
}
var CourseSelected: Bool {
return _courseSelected
}
var ClassStarted: Bool {
return _classStarted
}
required init?(map: Map){
}
func mapping(map: Map) {
_courseId <- map["courseId"]
_courseName <- map["courseName"]
_courseSelected <- map["selected"]
_classStarted <- map["classStarted"]
}
}
但由于某种原因,在我的responseObject中,我总是得到一个零对象。有人可以帮助我解决我的错误吗?
以下是获取回复的代码
func getElectivesCurriculumList(forProgramId programId: Int, batchYear: Int) {
let url = "someurl"
AlamofireManager.getAFSessionManager().request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseObject { (response: DataResponse<ElectivesMainResponse>) in
print("Electives API URL: \(url)")
switch response.result {
case .success(_ ):
if let value = response.result.value {
print("\(value.Res?.count)") //this is always nil
self.onGetElectivesResponse(value)
}
break
case .failure(let error):
print(error.localizedDescription)
self.onGetElectivesFailure(error.localizedDescription)
break
}
}
}