使用NSValue进行Swift NSCoding

时间:2015-06-18 23:01:03

标签: ios swift nscoding nsvalue

NSValue不是NSObject吗?那我为什么要崩溃

<!DOCTYPE html>
<html>
<head>
<style> 
p:nth-child(3n+0) {
    background: #ff0000;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
<p>The fifth paragraph.</p>
<p>The sixth paragraph.</p>
<p>The seventh paragraph.</p>
<p>The eight paragraph.</p>
<p>The ninth paragraph.</p>

<p><b>Note:</b> Internet Explorer 8 and earlier versions do not support the :nth-child() selector.</p>

</body>
</html>

错误记录

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

但如果我这样做

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

没有崩溃 - 两者都是NSValues ..对吗?

注意,我所有的其他NSCoding / Decoding工作正常。

1 个答案:

答案 0 :(得分:1)

我甚至没有达到你的目的。我只是撞了这两行:

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

这告诉我NSValue(MKCoordinate:)已被破坏。而且它不是斯威夫特的问题;我使用转换为Objective-C的相同代码得到同样的崩溃。