2
我有兩個確切的json對象,一個是從一個函數創建的,另一個是硬編碼的,硬編碼的工作,另一個沒有(它總是抱怨。關於錯誤invalid top-level type
,這是奇怪的任何尖端韓國社交協會swift 3,JSON,寫入時無效的頂級類型
let myData = self.dailyMileage?.toDictionary()
let directData = ["orgId" : self.orgId, "driverId" : self.driverId, "date" : Utils.getTodaysDate() ] as [String : Any]
//this won't work unless I substitute myData with directData
let jsonData = try JSONSerialization.data(withJSONObject: myData, options: .prettyPrinted)
//this is the function that produces myData, and nothing is nil
public func toDictionary() -> [String : Any] {
let dict = [ "orgId" : orgId , "driverId": driverId, "date" : date] as [String : Any]
return dict
}
問題因爲'myData'是可選的嗎? – rmaddy
您是否已將斷點放入jsonData行來檢查myData實例並確保它實際上是正確的? – TheValyreanGroup
@TheValyreanGroup是的,我這樣做,這就是爲什麼我說他們是完全一樣的 –