[swift-users] Possible swift 3 method rewrite issue with NSRequestConcreteImplementation ?

Tony Parker anthony.parker at apple.com
Mon Jul 18 19:25:56 CDT 2016


Hi John,

Can you try this?

@objc(encodeInt:forKey:)
override func encode(_ intv: Int, forKey key: String) {
            print("\(key) \(intv)") // never called
}

- Tony

> On Jul 18, 2016, at 12:11 AM, John Spurlock via swift-users <swift-users at swift.org> wrote:
> 
> Minimal repro case below.  I'm creating a custom NSCoder, but the "encodeInt:forKey" call is not dispatched properly to my overridden method when encoding an NSCalendar instance.
> 
> What am I missing here?  Without source code for any of this, I'm at a dead end.
> 
> Thanks,
> - john
> 
> ================================================
> 
>     private class FooCoder : NSCoder {
>         override var allowsKeyedCoding : Bool { return true }
>         
>         override func encode(_ objv: AnyObject?, forKey key: String) {
>             print("\(key) \(objv)") // called
>         }
>         
>         override func encode(_ intv: Int, forKey key: String) {
>             print("\(key) \(intv)") // never called
>         }
>     }
>     
>     func testCoder() {
>         Calendar.current.encode(with: FooCoder())  // crashes at runtime, result below
>     }
> 
> Results in:
> 
> "NSInvalidArgumentException", "*** -encodeInt:forKey: only defined for abstract class.  Define -[_TtCC22Tests22TestsP33_1961FooCoder encodeInt:forKey:]!"
> (
> 	0   CoreFoundation                      0x00007fff84da34f2 __exceptionPreprocess + 178
> 	1   libobjc.A.dylib                     0x00007fff883caf7e objc_exception_throw + 48
> 	2   CoreFoundation                      0x00007fff84e0a4bd +[NSException raise:format:] + 205
> 	3   Foundation                          0x00007fff903ccdbd NSRequestConcreteImplementation + 229
> 	4   Foundation                          0x00007fff9035323b -[NSCalendar(NSCalendar) encodeWithCoder:] + 251
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list