[swift-users] Decimal imported as NSDecimal not NSDecimalNumber in Swift 3 to Objective C

Chris Anderson christopher.anderson at gmail.com
Fri Nov 11 16:56:20 CST 2016


Sure thing. Yeah, ideally the bridging would be fixed, but at the least, correcting the documentation will be a good start. Will file, thanks.

Best,
Chris Anderson

> On Nov 11, 2016, at 5:55 PM, Tony Parker <anthony.parker at apple.com> wrote:
> 
> Hi Chris,
> 
> Can you file a radar or JIRA for us on this? It looks like something should be fixed in the documentation at least, or perhaps in the bridging.
> 
> - Tony
> 
>> On Nov 11, 2016, at 1:46 PM, Chris Anderson via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> I'm having problems with the type conversion between a Swift `Decimal` and an Objective C `NSDecimalNumber`.
>> 
>> If I have the Swift class:
>> 
>>     @objc class Exam: NSObject {
>>         var grade: Decimal = 90.0
>>     }
>> 
>> And try to use that Swift class in Objective C, 
>> 
>>     Exam *exam = [[Exam alloc] init];
>>     NSDecimalNumber *result = [[NSDecimalNumber zero] decimalNumberByAdding:grade.value];
>> 
>> I get the error:
>> 
>> Sending 'NSDecimal' to parameter of incompatible type 'NSDecimalNumber * _Nonnull'
>> 
>> as it seems like `grade` is being treated as an `NSDecimal` not an `NSDecimalNumber`. This seems incorrect as per https://developer.apple.com/reference/foundation/nsdecimalnumber <https://developer.apple.com/reference/foundation/nsdecimalnumber> it says 
>> 
>> "The Swift overlay to the Foundation framework provides the Decimal structure, which bridges to the NSDecimalNumber class. The Decimal value type offers the same functionality as the NSDecimalNumber reference type, and the two can be used interchangeably in Swift code that interacts with Objective-C APIs. This behavior is similar to how Swift bridges standard string, numeric, and collection types to their corresponding Foundation classes."
>> 
>> So I'm not sure if 1) I'm doing something wrong. 2) there's an error in the documentation or 3) this is a Swift bug. Number 1 on that list is definitely the most likely, but I wanted to see what I’m missing here.
>> 
>> I don't want to explicitly make the values in my Swift class `NSDecimalNumber` because then I cannot do simple arithmetic operations such as `+` without doing the whole ugly `decimalNumberByAdding` dance.
>> 
>> Thanks for the help!
>> 
>> Best,
>> Chris Anderson
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161111/d429ba48/attachment.html>


More information about the swift-users mailing list