存储在其中的类型AppStorage
必须符合RawRepresentable
. MKCoordinateRegion
不做这个开箱即用,但你可以添加一个扩展来增加一致性:
extension MKCoordinateRegion : RawRepresentable {
struct RepresentableForm : Codable {
var centerLat : Double
var centerLong : Double
var latDelta: Double
var longDelta : Double
}
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8), let result = try? JSONDecoder().decode(RepresentableForm.self, from: data)
else {
return nil
}
self = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: result.centerLat, longitude: result.centerLong),
span: MKCoordinateSpan(latitudeDelta: result.latDelta, longitudeDelta: result.longDelta)
)
}
public var rawValue: String {
do {
let data = try JSONEncoder().encode(RepresentableForm(centerLat: self.center.latitude, centerLong: self.center.longitude, latDelta: self.span.latitudeDelta, longDelta: self.span.longitudeDelta))
return String(data: data, encoding: .utf8) ?? ""
} catch {
fatalError()
}
}
}