[swift-users] Argument type 'Int' does not conform to expected type 'AnyObject'

Rick Mann rmann at latencyzero.com
Fri Sep 9 15:55:21 CDT 2016


I've seen old (pre-Swift 3) posts online (e.g. http://stackoverflow.com/questions/28920232/why-do-integers-not-conform-to-the-anyobject-protocol) that address this, but my code worked yesterday before I used Xcode 8 GM to migrate it to Swift 3, and now it doesn't, so I'm trying to understand what's going on.

In this case, I have this code:

open class HttpServer
{
    let clientSocketsLock = 0

    open func start()
    {
        ...some stuff...
        {
            HttpServer.lock(self.clientSocketsLock) //  ERROR HERE
            {
                self.clientSockets.remove(socket)
            }
        }
    }

    open class func lock(_ handle: AnyObject, closure: () -> ())
    {
        objc_sync_enter(handle)
        closure()
        objc_sync_exit(handle)
    }
}

I get the error "Argument type 'Int' does not conform to expected type 'AnyObject'" at the line marked above. What has changed here?  Did Int's behavior change? Is that described somewhere? I tried googling "Swift 3 Int" and variations without much luck.

I did see one discussion that talked about how when passing an Int to AnyObject, it was implicitly converted to NSNumber, and that this behavior only applied to certain types (e.g. not Int32). How can I tell when this kind of behavior exists? Is there a way to look at the type's declaration to see (e.g. it conforms to a protocol, or has a compiler attribute, or something)?

Thanks,

-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-users mailing list