尝试将swift对象解析为JSON数据时,我遇到了一个奇怪的问题。
长话短说:我的对象的属性都声明为Optional:
import Foundation
struct SrmaTaskStruct : JSONSerializable{
var workOrderID : Int?
var taskNum : Int?
var taskName: String?
var editable : Bool?
var project : SrmaProjectStruct?
var dates : [String]?
var realizacije : [RealizacijaStruct]?
var pctOfCompletionTask: CLong?
var creationDate: Date?
var createdBy: String?
var lastUpdateDate: Date?
var lastUpdatedBy: String?
var endEstimate: Date?
var endWork: Date?
var minuteEstimate: CLong?
var minuteWork: CLong?
var startEstimate: Date?
var startWork: Date?
var priority: CLong?
var text: String?
var solution: String?
var idRoleSpecialist: CLong?
var idRoleSpecialistAssign: CLong?
var idWorkType: CLong?
var idMetActivity: CLong?
var urgent: String?
init(workOrderID: Int?, taskNum : Int?, taskName : String?, editable : Bool?, project : SrmaProjectStruct, dates : [String]?, realizacije : [RealizacijaStruct]?) {
self.workOrderID = workOrderID
self.taskNum = taskNum
self.taskName = taskName
self.editable = editable
self.project = project
self.dates = dates
self.realizacije = realizacije
self.idMetActivity = 0
}
init(workOrderID: Int?, idMetActivity: CLong?, idWorkType: CLong?, urgent: String?, idRoleSpecialistAssign: CLong?, idRoleSpecialist: CLong?,
startEstimate: Date?, endEstimate: Date?, minuteEstimate: CLong?, pctOfCompletionTask: CLong?, text: String?) {
self.workOrderID = workOrderID
self.idMetActivity = idMetActivity
self.idWorkType = idWorkType
self.urgent = urgent
self.idRoleSpecialistAssign = idRoleSpecialistAssign
self.idRoleSpecialist = idRoleSpecialist
self.startEstimate = startEstimate
self.endEstimate = endEstimate
self.minuteEstimate = minuteEstimate
self.pctOfCompletionTask = pctOfCompletionTask
self.text = text
}
}
,在初始化期间,其中一些设置为非可选值:
var task = SrmaTaskStruct(workOrderID: (workOrder?.workOrderId)!, idMetActivity: CLong((discipline?.disciplineId)!), idWorkType: CLong((discipline?.workTypeId)!), urgent: urgent, idRoleSpecialistAssign: CLong((employer?.roleId)!), idRoleSpecialist: CLong((employee?.roleId)!), startEstimate: startEstimate!, endEstimate: endEstimate!, minuteEstimate: minuteEstimate!, pctOfCompletionTask: CLong(0), text: text!)
然后,我调用JSONSerializable toJSON():
let jsonData = task.toJSON()!
与this article一致的功能。结果,我得到了
function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A
异常,因为这段代码
guard JSONSerialization.isValidJSONObject(representation) else {
return nil
}
在toJSON()函数中返回nil。
对我来说,看起来JSONSerialization.isValidJSONObject()函数因为Optional属性而返回false,但我不明白为什么会发生这种情况,因为它适用于其他情况。
我错过了什么?有人可以帮忙吗?