简体   繁体   English

无法识别的选择器已发送到实例// swift 3

[英]Unrecognized selector sent to instance // swift 3

I'm trying to get data back from UserDefaults. 我正在尝试从UserDefaults取回数据。 I can store my code in it, but can't get it back. 我可以将代码存储在其中,但无法将其取回。

This is my CustomAnnotation class 这是我的CustomAnnotation类

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")
    }
}

And i'm getting it back like this 我就这样回来了

let decoded = defaults.object(forKey: "savedAnnotations") as! Data
let decodedAnnotations = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [CustomAnnotation]

Error: 错误:

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

After some research here, I couldn't find the answer. 经过一些研究后,我找不到答案。 I think it's about something optional but i'm not sure. 我认为这是可选的,但我不确定。

The error: unrecognized selector sent to instance means that you have called a method on an object which does not implement that method. 错误: unrecognized selector sent to instance意味着您已在未实现该方法的对象上调用了一个方法。 To fix your error, you should check the following – 要更正错误,您应检查以下内容–

The Tree class must conform to the NSCoding Protocol. Tree类必须符合NSCoding协议。 It should look something like class Tree: NSObject, NSCoding . 它看起来应该类似于class Tree: NSObject, NSCoding The Tree class also needs to implement required convenience init?(coder decoder: NSCoder) Tree类还需要实现required convenience init?(coder decoder: NSCoder)

Next, you cannot archive CLLocationCoordinate2D because it's not NSCoding compliant. 接下来,您不能存档CLLocationCoordinate2D因为它不符合NSCoding。 You can encode / decode the CLLocationCoordinate2D values like – 您可以编码/解码CLLocationCoordinate2D值,例如–

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


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

You should then delete latitude , longitude variables because they are unnecessary. 然后,您应该删除latitudelongitude变量,因为它们是不必要的。

Backing up for a minute, why are you storing a core data object in user defaults? 备份一分钟,为什么要在用户默认值中存储核心数据对象? Maybe understanding that will help me solve your issue. 也许理解会帮助我解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM