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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With