简体   繁体   中英

using swift extension from objc, and type difference

I'm getting the following error on simulator iphone6 and above.

Multiple methods named 'intValue' found with mismatched result, parameter type or attributes

on simulator 5 and below, there's no error.

Offending code is

int destinationType = [[args objectForKey:@"destinationType"] intValue];

I suspect it's due to the swift extension I added

 extension NSNull {

     func intValue() -> Int {
         return 0
     }
 }

So should I be worried about that the code won't compile on simulator 6 and above?

** EDIT **

It seems the difference is coming from the fact that iPhone 5 and less are 32bit and iPhone5s and above are 64bit.

So I should return more precise type than Int which can be either int32 or int64.

Now my code reads as following and wonder if its the right solution

     func intValue() -> Int32 {
         return 0
     }

The Objective-C method is expecting a C int . But a Swift Int is not a C int . Thus, specifying Int32 is right, because that is the same as int , and also because it is what other implementations of intValue return.

Of course, this brings up the fact that the method name intValue is already in use within Objective-C. Objective-C has no overloading, so if you are going to pick this name, you must match the existing signature (as you are now doing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM