Looks like my initial report was in error as I was trying to switch from NSNumber
to Decimal
on the Swift side.
This may actually be a documentation issue. The Swift version of the NSDecimalNumber
documentation says, at the very top:
An object for representing and performing arithmetic on base-10 numbers that bridges to Decimal; use NSDecimalNumber when you need reference semantics or other Foundation-specific behavior.
Also, lower down on the page, it says:
The Swift overlay to the Foundation framework provides the Decimal structure, which bridges to the NSDecimalNumber class. For more information about value types, see Working with Cocoa Frameworks in Using Swift with Cocoa and Objective-C (Swift 4.1).
In the Obj-C version of the docs:
The Swift overlay to the Foundation framework provides the NSDecimal
structure, which bridges to the NSDecimalNumber
class. For more information about value types, see Working with Cocoa Frameworks in Using Swift with Cocoa and Objective-C (Swift 4.1).
However, in a test project, this Obj-C type:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ObjCInfo : NSObject
@property (nonatomic) NSDecimalNumber *value;
@end
NS_ASSUME_NONNULL_END
has this Swift 4.2 interface, as shown in Xcode 10.1:
import Foundation
open class ObjCInfo : NSObject {
open var value: NSDecimalNumber
}
which seems to have no bridging at all!
However, this Swift type:
@objc
class Info: NSObject {
@objc
var value: Decimal
@objc
init(value: Decimal) {
self.value = value
}
}
generates this in the -Swift.h
:
SWIFT_CLASS("_TtC13DecimalTester4Info")
@interface Info : NSObject
@property (nonatomic) NSDecimal value;
- (nonnull instancetype)initWithValue:(NSDecimal)value OBJC_DESIGNATED_INITIALIZER;
- (nonnull instancetype)init SWIFT_UNAVAILABLE;
+ (nonnull instancetype)new SWIFT_DEPRECATED_MSG("-init is unavailable");
@end
Attempting to run this code:
let objCInfo = ObjCInfo()
objCInfo.value = 10
let swiftInfo = Info(value: 10)
swiftInfo.value = objCInfo.value
results in the error: 'NSDecimalNumber' is not implicitly convertible to 'Decimal'; did you mean to use 'as' to explicitly convert?
Try to do the opposite:
objCInfo.value = swiftInfo.value
results in the error: Cannot assign value of type 'Decimal' to type 'NSDecimalNumber'
, suggesting to initialize the NSDecimalNumber
with swiftInfo.value
.
The errors are expected, if no bridging is occurring between NSDecimalNumber
and Decimal
. At this point I'm confused, as it appears there's no bridging at all for NSDecimalNumber
.