2017-04-22 18 views
2

私は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 

答えを見つける。私はそれは何かについてのオプションだと思うが、わからない。

+3

エラーを確認します。あなたの 'Tree'クラスについてです。 'NSCoding'にも準拠する必要があります。 – rmaddy

+0

無関係ですが、このようなデータを 'UserDefaults'に保存しないでください。データをファイルに書き込みます。 – rmaddy

+0

'Tree'は' NSCoding'に準拠していますか? – ozgur

答えて

0

エラー:unrecognized selector sent to instanceは、そのメソッドを実装していないオブジェクトに対してメソッドを呼び出したことを意味します。エラーを修正するには、次の項目を確認する必要があります。

ツリークラスは、NSCodingプロトコルに準拠している必要があります。それはclass Tree: NSObject, NSCodingのように見えるはずです。 Treeクラスも実装する必要がありますrequired convenience init?(coder decoder: NSCoder)

次に、CLLocationCoordinate2DはNSCodingに準拠していないためアーカイブできません。彼らは不要なので、あなたが、その後latitudelongitude変数を削除する必要があります

encode(coordinate.latitude, forKey: "latitude") 
encode(coordinate.longitude, forKey: "longitude") 


decodeDouble(forKey: "latitude") 
decodeDouble(forKey: "longitude") 
self.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 

- あなたのようなCLLocationCoordinate2D値をデコード/エンコードすることができます。

1分後にバックアップすると、なぜユーザーのデフォルトでコアデータオブジェクトが格納されていますか?あなたの問題を解決するのに役立つかもしれません。

+0

返信いただきありがとうございます!私は、次の理由により、ユーザーのデフォルトでコアデータオブジェクトを格納しようとしています。 リクエストをしてツリーを戻すアプリを作っています。これらのツリーには、文字列形式の場所があります。次に、ジオコーダーを使用してその文字列アドレスを座標に変換し、注釈を配置します。しかし、それはしばらく時間がかかります。だから、私はuserdefaultsにすべてのアノテーションを保存して、アプリが常にジオコードする必要はないようにしたい。 これはいくつかのものをクリアしたいと考えています –

関連する問題