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

John Spurlock john.spurlock at gmail.com
Mon Jul 18 20:09:24 CDT 2016


Hi Tony - when I add that attribute, I get an error at compile-time:
Objective-C method has a different selector from the method it overrides
('encodeInt:forKey:' vs. 'encodeInteger:forKey:')

If I update to encodeInteger:forKey as the fix describes, it compiles, but
I'm getting the same original problem at runtime.  i.e. "encodeInt:forKey:
only defined for abstract class"

Any other ideas?  See the same thing over there?  You should be able to
paste that into a new swift 3 test.

On Mon, Jul 18, 2016 at 8:25 PM, Tony Parker <anthony.parker at apple.com>
wrote:

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160718/992c666d/attachment.html>


More information about the swift-users mailing list