Argument type 'Int' does not conform to expected type 'AnyObject'


(Rick M) #1

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@latencyzero.com


(Kevin Nattinger) #2

You’ll need to explicitly add the “as NSNumber” now.

https://github.com/apple/swift-evolution/blob/master/proposals/0072-eliminate-implicit-bridging-conversions.md

···

On Sep 9, 2016, at 1:55 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:

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@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Joe Groff) #3

Or even better, you should now be able to use `Any` in Swift 3 in most places Swift 2 required `AnyObject`. Swift now handles the object conversion as part of the runtime Objective-C bridge, so these weird special-case implicit conversions should not be necessary in most cases anymore.

-Joe

···

On Sep 9, 2016, at 2:02 PM, Kevin Nattinger via swift-users <swift-users@swift.org> wrote:

You’ll need to explicitly add the “as NSNumber” now.

https://github.com/apple/swift-evolution/blob/master/proposals/0072-eliminate-implicit-bridging-conversions.md


(Rick M) #4

Thank you both for the answers.

In the end, I changed the method to take "Any" instead of "AnyObject", since it just passed it on to a system call that accepted "Any".

···

On Sep 9, 2016, at 15:46 , Joe Groff <jgroff@apple.com> wrote:

On Sep 9, 2016, at 2:02 PM, Kevin Nattinger via swift-users <swift-users@swift.org> wrote:

You’ll need to explicitly add the “as NSNumber” now.

https://github.com/apple/swift-evolution/blob/master/proposals/0072-eliminate-implicit-bridging-conversions.md

Or even better, you should now be able to use `Any` in Swift 3 in most places Swift 2 required `AnyObject`. Swift now handles the object conversion as part of the runtime Objective-C bridge, so these weird special-case implicit conversions should not be necessary in most cases anymore.

--
Rick Mann
rmann@latencyzero.com