我使用 Alamofire 创建了APIService,当我使用 print函数在控制台中显示内容时,我成功从下面的代码行中获取了值。
if let passcodeJSON = response.result.value {
let passcodeObj = passcodeJSON as! [String : Any]
但是参考下面的图片,我的代码中出现了线程1:信号SIGABRT ,并且无法将类型'__NSSingleObjectArrayI'(0x10afea608)的值强制转换为'NSDictionary'(0x10afeb288) 。
如何解决此问题?我找不到适用于我的问题的解决方案。希望你能给我一部分时间。谢谢。
APIService
func validatePasscode(passcode: String, completion: @escaping (Event?) -> Void)
{
if let passcodeURL = URL (string: "\(PASSCODE_CHECKER_URL)/\(passcode)") {
Alamofire.request(passcodeURL, method: .get).responseJSON { (response) in
if let passcodeJSON = response.result.value {
let passcodeObj = passcodeJSON as! [String : Any]
let eventIDObj = passcodeObj["event_id"] as! [String : Any]
let event = Event.init(JSON: eventIDObj)
completion(event)
}else {
completion(nil)
}
}
print("Loading from web.....")
}
}
}
Event.swift
class Event {
let id: String?
let name: String?
let location: String?
let startDateTime: Date?
let endDateTime: String?
let deleteFlag: Bool?
let deleteDateTime: String?
let dateCreated: String?
let hasRaffle: Bool?
let registrationReq: Bool?
let participantCount: Int
let closedFlag: Bool?
let closedDateTime: String?
let reopenFlag: Bool?
let reopenDateTime: String?
init?(JSON: [String: Any]) {
guard let eventID = JSON["event_id"] as? String,
let eventName = JSON["event_name"] as? String,
let eventLocation = JSON["event_location"] as? String,
let startDateTime = JSON["start_datetime"] as? String,
let endDateTime = JSON["end_datetime"] as? String,
let participantCount = JSON["participant_count"] as? Int else {
return nil
}
self.id = eventID
self.name = eventName
self.location = eventLocation
self.endDateTime = endDateTime
self.participantCount = participantCount
if let formattedStartDateTime = getDateFromString(dateString: startDateTime, formatString: "yyyy-MM-dd'T'HH:mm:ss.SSS") {
self.startDateTime = formattedStartDateTime
}else {
self.startDateTime = Date()
}
if let deleteFlag = JSON["delete_flag"] as? Bool {
self.deleteFlag = deleteFlag
}else {
self.deleteFlag = nil
}
if let deletedDateTime = JSON["deleted_datetime"] as? String {
self.deleteDateTime = deletedDateTime
}else {
self.deleteDateTime = nil
}
if let dateCreated = JSON["date_created"] as? String {
self.dateCreated = dateCreated
}else {
self.dateCreated = nil
}
if let hasRaffle = JSON["hasRaffle"] as? Bool {
self.hasRaffle = hasRaffle
}else {
self.hasRaffle = nil
}
if let registrationReq = JSON["registration_req"] as? Bool {
self.registrationReq = registrationReq
}else {
self.registrationReq = nil
}
if let closedFlag = JSON["closed_flag"] as? Bool {
self.closedFlag = closedFlag
}else {
self.closedFlag = nil
}
if let closedDateTime = JSON["closed_datetime"] as? String {
self.closedDateTime = closedDateTime
}else {
self.closedDateTime = nil
}
if let reopenFlag = JSON["reopen_flag"] as? Bool {
self.reopenFlag = reopenFlag
}else {
self.reopenFlag = nil
}
if let reopenDateTime = JSON["reopen_datetime"] as? String {
self.reopenDateTime = reopenDateTime
}else {
self.reopenDateTime = nil
}
}
}
JSON
`SUCCESS: (
{
"closed_datetime" = "<null>";
"closed_flag" = 0;
"date_created" = "2018-07-12T12:53:00.000";
"delete_flag" = 0;
"deleted_datetime" = "<null>";
"end_datetime" = "2018-07-13T13:00:00.000";
"event_id" = "707024c4-8b93-46d6-b34d-e95b53b45750";
"event_location" = "IT Room";
"event_name" = "Laugh Trip";
hasRaffle = 0;
"participant_count" = 6;
"registered_participants_count" = 3;
"registration_req" = 0;
"reopen_datetime" = "<null>";
"reopen_flag" = 0;
"start_datetime" = "2018-07-13T12:00:00.000";
}
)
尝试后
if let passcodeJSON = response.result.value as? [[String:Any]],
let passcodeObj = passcodeJSON.first { let event = Event.init(JSON: passcodeObj);
completion(event) }
passcodeObj: [字符串:任意] 140256192790856键/值对
出现在调试区域中。似乎加载没有尽头。