在 MapView 上保存注释

Saving Annotations on MapView

目前我正在开发的应用程序创建了自定义注释,但是当用户从带有 mapView 的页面切换时,它们都被卸载了。我将它们保存到自定义数组中,但 UserDefaults 不允许我保存该数组。有没有一种简单的方法来保留放置的注释,或者我如何让 UserDefaults 保存我的数组。我的数组是这样的:

var mapAnnotations = [pinLocations]()
struct pinLocations {
    let latitude: Double
    let longnitude: Double
    let name: Double
    let time: String
}

让你的结构成为 class,派生自 NSObject,并通过实现 init?(coder:)encode(with:) 采用 NSCoding。这个 class 的数组现在可以通过调用 NSKeyedArchiver.archivedData(withRootObject:) 归档到 NSData 并保存到 UserDefaults 中(通过调用 NSKeyedUnarchiver.unarchiveObject(with:) 相反)。