私はUserDefaultsからデータを取得しようとしています。 自分のコードを保存することはできますが、元に戻すことはできません。インスタンスに送信された認識できないセレクタ// swift 3
これは
import UIKit
import MapKit
class CustomAnnotation: NSObject, NSCoding, MKAnnotation {
var tree:Tree!
var title:String?
var coordinate: CLLocationCoordinate2D
var latitude:CLLocationDegrees
var longitude:CLLocationDegrees
init(coordinate:CLLocationCoordinate2D, title:String, tree:Tree) {
self.tree = tree
self.title = title
self.coordinate = coordinate
self.latitude = coordinate.latitude
self.longitude = coordinate.longitude
}
required init? (coder aDecoder: NSCoder){
self.tree = aDecoder.decodeObject(forKey: "tree") as! Tree
self.title = aDecoder.decodeObject(forKey: "title") as? String
self.coordinate = (aDecoder.decodeObject(forKey: "coordinate") as? CLLocationCoordinate2D)!
self.latitude = (aDecoder.decodeObject(forKey: "latitude") as? CLLocationDegrees)!
self.longitude = (aDecoder.decodeObject(forKey: "longitude") as? CLLocationDegrees)!
}
func encode(with aCoder: NSCoder) {
aCoder.encode(tree, forKey: "tree")
aCoder.encode(title, forKey: "title")
aCoder.encode(latitude, forKey: "latitude")
aCoder.encode(longitude, forKey: "longitude")
aCoder.encode(coordinate,forKey:"coordinate")
}
}
私CustomAnnotationクラスであると私はこの
let decoded = defaults.object(forKey: "savedAnnotations") as! Data
let decodedAnnotations = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [CustomAnnotation]
エラーのようにそれを取り戻すよ:私はできませんでした、ここではいくつかの研究の後
2017-04-22 19:01:08.751710+0200 Sam_Goeman_Multec_werkstuk2v2[3665:981692] -[Tree initWithCoder:]: unrecognized selector sent to instance 0x17447ee00
2017-04-22 19:01:08.751923+0200 Sam_Goeman_Multec_werkstuk2v2[3665:981692] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Tree initWithCoder:]: unrecognized selector sent to instance 0x17447ee00'
*** First throw call stack:
(0x182ed2fd8 0x181934538 0x182ed9ef4 0x182ed6f4c 0x182dd2d2c 0x183922420 0x183921b58 0x10001c120 0x10001cd04 0x183922420 0x183928f00 0x1838be83c 0x183922420 0x183921b58 0x183920d84 0x100014de8 0x100011360 0x100011588 0x189001f9c 0x1890ba0c4 0x1890b9f9c 0x1890b92cc 0x1890b8d00 0x1890b88b4 0x1890b8818 0x188fff158 0x1861ef274 0x1861e3de8 0x1861e3ca8 0x18615f360 0x1861863c0 0x186186e8c 0x182e809a0 0x182e7e628 0x182daedb4 0x18906c45c 0x189067130 0x10001a51c 0x181dbd59c)
libc++abi.dylib: terminating with uncaught exception of type NSException
答えを見つける。私はそれは何かについてのオプションだと思うが、わからない。
エラーを確認します。あなたの 'Tree'クラスについてです。 'NSCoding'にも準拠する必要があります。 – rmaddy
無関係ですが、このようなデータを 'UserDefaults'に保存しないでください。データをファイルに書き込みます。 – rmaddy
'Tree'は' NSCoding'に準拠していますか? – ozgur