Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call an ObjC method taking int in Swift because Int32 cannot be converted to Int

I'm working on a mixed Objective-C and Swift project. I have a class named Point, defined in Objective-C, which has int properties and an initializer:

@property (nonatomic) int column;
@property (nonatomic) int row;
- (id) initWithColumn:(int)column row:(int)row;

which I use to create an object in a Swift file:

Point(column: Column, row: Row)

But it gives an error:

cannot invoke 'init' with an argument list with type '(column: Int, row: Int)'

By checking further, I found out that these int properties in Objective-C become Int32 in Swift, and Int32 cannot be converted to Int. Does anyone know how to fix this?

I wonder how comes that Objective C int becomes Int32 in Swift and how to deal with this so that I can have Int type in Swift from Objective C? I use this class frequently and prefer to keep the type consistent instead of casting each time.

like image 687
Xi Zhu Avatar asked Oct 27 '25 02:10

Xi Zhu


1 Answers

You should probably avoid using int in Objective-C and use NSInteger instead, that gets mapped to Swift's Int.

Anyway, if you need to convert an Int variable, say n, to Int32, you can do it with

var n32: Int32 = Int32(n)

so your code would be

Point(column: Int32(Column), row: Int32(Row))
like image 148
Marcos Crispino Avatar answered Oct 29 '25 19:10

Marcos Crispino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!