3
votes

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.

2

2 Answers

6
votes

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))
0
votes

All you have to do is cast from one type to the other.

Try: Point(column: int(Column), row: int(Row))

BTW, it is non-standard/confusing to name variable with a name beginning with a upper-case letter in Swift/Objective-C.