有没有人想过让GSMPlace在googleplace上变得可编码?
我想要的是,将GMSPlace数据(地点和名称)存储到UserDefaults中,然后从UserDefaults中检索并存储回destinationPlace。
import Foundation
import RxSwift
import RxCocoa
import GooglePlaces
class SearchViewModel {
var destinationPlace: BehaviorRelay<GMSPlace?> = BehaviorRelay(value: nil)
var isValid: Bool {
if destinationPlace.value != nil {
return true
}
return false
}
}发布于 2021-08-05 11:29:01
我不知道GooglePlaces或对象类型GMSPlace的任何细节。
但是在Swift中,如果你想给一个类添加一些东西,你可以创建一个扩展,有选择地给它添加协议和添加函数。在您的示例中,它将如下所示:
extension GMSPlace: Codable {
enum CodingKeys: String, CodingKey {
case propertyName1
case propertyName2
...
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
propertyName1 = try container.decode(String.self, forKey: .propertyName1)
propertyName2 = try container.decode(Int.self, forKey: .propertyName2)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(propertyName1, forKey: .propertyName1)
try container.encode(propertyName2, forKey: .propertyName2)
}
}你必须弄清楚你需要包含哪些属性,它们需要映射到什么类型等等。
您还应该为他们打开一个票证,以便在给您造成问题的情况下将其添加到SDK本身
https://stackoverflow.com/questions/68663034
复制相似问题