<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><blockquote type="cite" class="">On 29 Dec 2015, at 10:54 AM, Philippe Hausler <<a href="mailto:phausler@apple.com" class="">phausler@apple.com</a>> wrote:<br class=""><br class="">It is ok on the special types; those are harder than they may seem… NSValue in Darwin has some limitations like that especially when it comes to secure coding or non standard aligned values.</blockquote><div class=""><br class=""></div><div class="">I have a solution that’s not <i class="">too</i> inelegant, see:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/lhoward/swift-corelibs-foundation/blob/lhoward/nscoding/Foundation/NSSpecialValue.swift" class="">https://github.com/lhoward/swift-corelibs-foundation/blob/lhoward/nscoding/Foundation/NSSpecialValue.swift</a></div><div class=""><br class=""></div><div class="">This takes advantage of the fact that structures can implement protocols in Swift. NSRect, etc implement NSCoding (by way of NSSpecialValueCoding). The structures are then boxed by NSSpecialValue (a subclass of NSValue). It would have been nice to make NSSpecialValue a generic class but unfortunately it doesn’t appear you can specialise at runtime, which the unarchiver would need to do.</div><div class=""><br class=""></div><div class="">e.g.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>CGPoint<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">: </span>NSSpecialValueCoding<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>?(coder aDecoder: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">NSCoder</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> aDecoder.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">allowsKeyedCoding</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> = aDecoder.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">decodePointForKey</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"NS.pointval"</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> = aDecoder.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">decodePoint</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> encodeWithCoder(aCoder: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">NSCoder</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> aCoder.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">allowsKeyedCoding</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> aCoder.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">encodePoint</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>, forKey: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"NS.pointval"</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> aCoder.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">encodePoint</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> supportsSecureCoding() -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>true</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> objCType() -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>"{CGPoint=dd}"</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> isEqual(aValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Any</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> other = aValue <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span>? <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">CGPoint</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> other == <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">false</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">There’s still NSConcreteValue for ObjC type representations but I found it difficult to merge the two (for reasons I can explain later).</div><div class=""><br class=""></div><div class="">Happy new year!</div><div class=""><br class=""></div>— Luke<br class=""></body></html>