Swift NSCoding with NSValue

599 views Asked by At

Isn't NSValue an NSObject ? So why am I crashing on

    var coordinatesRawData = NSValue(MKCoordinate: coordinates.first!)
    if coordinatesRawData != nil {
        // Crashing here. I have a valid NSValue object
        aCoder.encodeObject(coordinatesRawData, forKey: "coordinatesRawData")
    }

Error Log

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedArchiver encodeValueOfObjCType:at:]: this archiver cannot encode structs'

But if I do this

    var coordinatesRawData = NSValue()
    aCoder.encodeObject(coordinatesRawData, forKey: "coordinatesRawData")

There is no crash - Both are NSValues .. right ?

Note, all my other NSCoding / Decoding is working fine.

1

There are 1 answers

5
matt On BEST ANSWER

I didn't even get as far as you did. I crashed on just these two lines:

let loc = CLLocationCoordinate2D(latitude: 20, longitude: 20)
let val = NSValue(MKCoordinate:loc)

This tells me that NSValue(MKCoordinate:) is broken. And it's not a Swift issue; I get the same crash using the same code translated into Objective-C.