Hello
I want to make universal json exporter structure, and Swift drop my venture.
The code below
import Foundation
public protocol Sizeble {
var weight: Double { get }
var widht: Double { get }
var height: Double { get }
}
public struct Bike: Sizeble {
public var weight: Double = 100.0
public var widht: Double = 15
public var height: Double = 5
public init() {}
}
public struct ExportWraper<Base>{
public var base: Base
public init(base: Base) {
self.base = base
}
}
extension ExportWraper where Base: Sizeble {
public struct Export: Codable {
public let weight: Double
public let width: Double
public let height: Double
init(base: Base) {
weight = base.weight
width = base.widht
height = base.height
}
}
public var export: Export { return Export(base: base) }
}
let bike = Bike()
let wraper = ExportWraper(base: bike)
let exporter = wraper.export
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
do {
let data = try encoder.encode(exporter)
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the
state before expression evaluation.
} catch let error {
print(error)
}
What I do wrong?
Hi Alex, it doesn't look like you're doing anything wrong; this works fine for me in Swift 4.1. Adding a print line
let data = try encoder.encode(exporter)
print(String(data: data, encoding: .utf8)!)
results in
{
"width" : 15,
"height" : 5,
"weight" : 100
}
If you are able to reproduce this consistently, do you mind please filing a bug report so we can investigate?
Thanks. I'm still on Swift 4.0.3. Tomorrow will update to 4.1. What is best way to update, just install Xcode beta?
Yes, though this shouldn't happen in Swift 4.0.3 either... Please update this if you still see it in 4.1.