当我试图解码这个json时,我得到了这个错误:
[{
"id": "76f22c25-cee7-4c7a-94fa-1fb85720f580",
"purchaseDate": "2012-04-05T19:03:43Z",
"title": "azare",
}, {
"id": "9b4b9f7d-382f-4555-9eaa-97939b13633f",
"purchaseDate": "2012-04-05T19:02:46Z",
"title": "Chocolat",
}, {
"id": "02a0aa06-2d0c-4ab9-aaaa-af7dee7b4845",
"purchaseDate": "2012-09-24T17:39:52Z",
"title": "Some thing",
}]
致命错误:'尝试!'表达式意外地引发了一个错误:Swift.DecodingError.typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath:[Foundation。(_ JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue:"索引0",intValue:可选(0)),MyApp.Purchase.CodingKeys.purchaseDate], debugDescription:"预计解码Double但找到一个字符串/数据 相反。",underlyingError:nil)):file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.69.2/src/swift/stdlib/public/core/ErrorType.swift, 第181行
My code:
//Purchase+CoreDataClass.swift
@objc(Purchase)
public class Purchase: NSManagedObject, Codable {
enum CodingKeys: String, CodingKey {
case id
case title
case purchaseDate
}
required convenience public init(from decoder: Decoder) throws {
var context : NSManagedObjectContext = MyAppCoreDataManager.sharedInstance.persistentContainer.viewContext
guard let entity = NSEntityDescription.entity(forEntityName: "Purchase", in: context!) else { fatalError() }
self.init(entity: entity, insertInto: context!)
let container = try decoder.container(keyedBy: CodingKeys.self)
self.title = try container.decodeIfPresent(String.self, forKey: .title)
self.id = try container.decodeIfPresent(String.self, forKey: .id)
self.purchaseDate = try! container.decodeIfPresent(Date.self, forKey: .purchaseDate)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(title, forKey: .title)
try container.encode(purchaseDate, forKey: .purchaseDate)
}
}
// Purchase+CoreDataProperties.swift
extension Purchase {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Purchase> {
return NSFetchRequest<Purchase>(entityName: "Purchase")
}
@NSManaged public var id: String?
@NSManaged public var title: String?
@NSManaged public var purchaseDate: Date?
}
//CALL
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .base64
let purchases = try decoder.decode([Purchase].self, from: value)
If i remove "purchaseDate" everything works correctly
答案 0 :(得分:2)
存在一般性误解,错误大约是Data
而不是TimeInterval
发生错误是因为默认情况下JSON解码器不会解码ISO8601日期。默认值为Double
又名Double
。
错误消息的内容是:它需要String
,但找到了DateDecodingStrategy
您必须将解码器的.iso8601
设置为decoder.dateDecodingStrategy = .iso8601
try!
删除function save($image){
header("Content-type: image/png");
$name = "png";
$save = "./image/png/". strtolower($name) ."_.png";
imagepng($image, $save);
// cmBn: c = not critical; m = private; B must be capital; n = depends on image data
$chunk = "cmBn";
$build_id = 2139; // Building ID (Uint32), positive values 32-bit up to 4,294,967,295 (232 − 1)
$floor_id = 12345; // Floor ID (Uint8), positive values 8-bit up to 255
$max_lat = 51.897867564536452; // Max Latitude (Float)
$min_long = 13.1234567890123; // Min Longitude (Float)
$min_lat = 51.97867564518453; // Min Latitude (Float)
$max_long = 13.2223334445551; // Max Longitude (Float)
// insert a BKGD chunk into the PNG file for graceful image degradation in IE6
$pngData = bin2hex(file_get_contents($save));
$idatMarker = '200049444154'; //?
$bkgdMarker = '624b4744'; //cmBn
$bkgdChunk = '0006' . $bkgdMarker; //?
//oreach ($bgColor as $bit)
//{
//$bkgdChunkData .= '00' . dechex($bit);
$bkgdChunkData .= '00' . dechex($build_id);
$bkgdChunkData .= '00' . dechex($floor_id);
$bkgdChunkData .= '00' . dechex($max_lat);
$bkgdChunkData .= '00' . dechex($min_long);
$bkgdChunkData .= '00' . dechex($min_lat);
$bkgdChunkData .= '00' . dechex($max_long);
//}
$bkgdChunk .= $bkgdChunkData;
$bkgdChunk .= dechex(crc32(pack('H*', $bkgdMarker . $bkgdChunkData))) . '0000';
$parsed = explode($idatMarker, $pngData, 2); // split file by first 'IDAT' chunk
$pngData = pack('H*', $parsed[0] . $bkgdChunk . $idatMarker . $parsed[1]);
file_put_contents($save, $pngData);
}
中的感叹号以移交错误