2

I can't bridge ObjectiveC long type into Swift.

//Swift
typealias Long = Int64
struct Item: Equatable {
    let itemId: Long
    ...
}

//ObjC
@interface MyObject : NSObject
@property(nonatomic, assign) long itemId;

//Try create object in Swift
Item(itemId: objCObject.itemId) 

Error: Cannot convert value of type 'Int' to expected argument type 'Long' (aka 'Int64')

ArisRS
  • 1,362
  • 2
  • 19
  • 41
  • Does this answer your question? [Can I cast Int64 directly into Int?](https://stackoverflow.com/questions/32793460/can-i-cast-int64-directly-into-int) – pkamb Sep 03 '20 at 13:32

1 Answers1

2

The (Objective-)C type long is bridged to Swift as CLong which is an alias for the Swift Int type. Int can be a 32-bit or 64-bit integer, depending on the platform.

In any case, you cannot cast or assign an Int64 directly to an Int, see also Can I cast Int64 directly into Int?.

If you want to keep the Swift itemID as a Int64 then you'll have to convert it to CLong when calling the Objective-C method. Alternatively you can change the Swift type to make it compatible:

struct Item: Equatable {
    let itemId: CLong
    ...
}

Another option would be to change the Objective-C type to

@property(nonatomic, assign) int64_t itemId;

which is then imported to Swift as Int64.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382