Got Unrecognized Selector -Replacementobjectforkeyedarchiver: Crash When Implementing Nscoding in Swift

NSCoder: Unrecognized selector sent to instance

With the help of a couple of users and some added searching, I was able to figure out the cause of my problem.

Per the suggestion of the previously mentioned users, I added the NSCoding protocol to my CalendarEvent class.

class CalendarEvent : NSObject, NSCoding {
....
}

Unfortunately, that didn't completely fix the issue, but it was a start. A few errors and a quick search showed me that I had to change this:

init(withCoder coder : NSCoder){
....
}
func encodeWithCoder(coder : NSCoder){
....
}

to this:

required init(coder : NSCoder) {
....
}
func encode(with aCoder : NSCoder) {
....
}

After making those changes, my code ran perfectly fine.

Unrecognized selector sent to instance while archiving data (NSCoding)

You need to implement NSCoding protocol inside your Furniture object:

- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.yourpoperty forKey:@"PROPERTY_KEY"];
}

-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
self.yourpoperty = [aDecoder decodeObjectForKey:@"PROPERTY_KEY"];
}
return self;
}

Basically you specify what should be written (encoded) and read from a file (decoded). Usually for each property you want to store in a file, you make same as I did here in an example.

Swift 3 Xcode 8 - SwiftValue encodeWithCoder - unrecognized selector sent to instance

The problem is that you are trying to archive an optional. Replace this line:

if (object != nil) {

with:

if let object = object {

Crash due to unrecognised selector on MKPointAnnotation

The issue was coming as I was selecting the annotation which wasn't visible on the map. I was cluttered.

I solved the issue by first zooming to the location and then setting the annotation property as selected.

So I updated the mapViewAnimateTo method.

func mapViewAnimateTo(_ index: Int) {
print("MapView scrolling to index \(index)")
let point = presenter.getCollectedPoints(index).coordinates
let coordinate = CLLocationCoordinate2D(latitude: point[0].latitude, longitude: point[0].longitude)

CATransaction.begin()
CATransaction.setCompletionBlock({
// 2. Upon completion of zooming, marks the first annotation found at respective coordinate as selected.
if let annotation = self.mapView.annotations.filter ({ $0.coordinate.latitude == coordinate.latitude && $0.coordinate.longitude == coordinate.longitude }).first {
self.mapView.selectAnnotation(annotation, animated: true)
}
})
// 1. Zooms to the respective coordinate.
mapView.animateToPoint(coordinate, animated: true)
CATransaction.commit()
}

Even updated the distance of the region to 10 meters.

extension MKMapView {
func animateToPoint(_ coordinate: CLLocationCoordinate2D, animated: Bool) {
let coordinateRegion = MKCoordinateRegion(center: coordinate, latitudinalMeters: 10, longitudinalMeters: 10)
self.setRegion(coordinateRegion, animated: animated)
}
}

unrecognized selector sent to instance for NSKeyedUnarchiver

The error message shows that

Thecontacts = [valueDictionary objectForKey:@"Contacts"];

returns a NSString object, not a NSData object as you expected. I assume that you created
the JSON using something like

[NSString stringWithFormat:@"%@", Contacts]

which uses the description method of NSData and returns a string like


<62706c69 73743030 d4010203 0405082b ... >

or perhaps SBJson does that implicitly. You could parse that string and convert
it back to NSData. But that would be a fragile approach because the actual
description format of NSData is not documented and might change in the future.

Note that JSON does not know "raw binary data", only dictionaries, arrays, strings and numbers. You should convert the NSData to NSString (using one of the publicly available
Base64 converters) and store the Base64 string in the JSON.



Related Topics



Leave a reply



Submit