[swift-users] Decimal imported as NSDecimal not NSDecimalNumber in Swift 3 to Objective C
Philippe Hausler
phausler at apple.com
Mon Nov 14 17:07:19 CST 2016
Seems like you are missing some bridges here since NSDecimalNumber’s signature takes an object
let value = NSDecimalNumber(value: 2)
let test = 2.0 // double
let product = value.multiplying(by: 2.0) // compiles
let x = value.multiplying(by: Decimal(2.0) as NSDecimalNumber) // compiles
let y = value.multiplying(by: NSDecimalNumber(value: Double(2.0))) // compiles
let z = value.multiplying(by: NSDecimalNumber(value: test)) // compiles
Those methods perhaps should have been imported as their bridged types but again it is an ambiguity since we should really distinguish between NSDecimalNumber and NSDecimal interfaces in objc and so consequently they have to be distinguished in Swift as NSDecimalNumber and Decimal.
> On Nov 14, 2016, at 1:59 PM, Chris Anderson <christopher.anderson at gmail.com> wrote:
>
> Thanks for that follow up, I’m still a little confused at why one direction works and the other does not, but I’m getting there.
>
> I’ve found another issue I’ll bug report, but it’s along the same lines and wanted to run it by this thread. If I have an NSDecimalNumber, in Swift, and perform math on a literal value, (product) the code compiles. If I assign that value to a variable, or use any of the other Decimal/Double types, I cannot compile. I would expect a Double to not work, but I would expect ‘Decimal’ to work, in this case, as I’m not crossing the Objective C border. And, I’m confused how using the literal ‘2.0’ is interpreted as an NSDecimalNumber, and works in the ‘product’ stop, but I would expect the compiler to try and make it into a Double, as it does on the ‘test’ variable.
>
> let value = NSDecimalNumber(value: 2)
> let test = 2.0 // double
> let product = value.multiplying(by: 2.0) // compiles
> let x = value.multiplying(by: Decimal(2.0)) // does not compile
> let y = value.multiplying(by: Double(2.0)) // does not compile
> let z = value.multiplying(by: test) // does not compile
>
> Best,
> Chris Anderson
>
>> On Nov 11, 2016, at 6:07 PM, Philippe Hausler <phausler at apple.com <mailto:phausler at apple.com>> wrote:
>>
>>
>> NSDecimal is not toll free bridged, but it does have a bridge to NSDecimalNumber.
>>
>> So take this for example:
>>
>> @objc class Exam: NSObject {
>> var grade: Double = 90.0
>> }
>>
>> It would be reasonable to expect that is exposed in objc as:
>>
>> @interface Exam : NSObject
>> @property double grade;
>> @end
>>
>> and not:
>>
>> @interface Exam : NSObject
>> @property NSNumber *grade;
>> @end
>>
>> As it stands this is exposing as the structural type since that structural type comes from objective-c. Unlike String or Dictionary that have direct counterparts - NSDecimal and NSDecimalNumber both are sourced from the objective-c headers. That being said an API exposed in objc as returning a NSDecimalNumber should be exposed into swift as returning a Decimal (the struct NSDecimal). So if Exam was implemented in objc as such:
>>
>> @interface Exam : NSObject
>> @property NSDecimalNumber *grade;
>> @end
>>
>> that should be imported into swift as:
>>
>> class Exam : NSObject {
>> var grade : Decimal
>> }
>>
>>> On Nov 11, 2016, at 2:58 PM, Adam C. Lickel via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>>
>>> NSDecimal has toll-free bridging with NSDecimalNumber so you can still do as casting when talking to an Objective-C API.
>>>
>>>> On Nov 11, 2016, at 2:56 PM, Chris Anderson via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>>>
>>>> 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 <mailto: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 <https://lists.swift.org/mailman/listinfo/swift-users>
>>>>>
>>>>
>>>> _______________________________________________
>>>> swift-users mailing list
>>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>>>
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161114/da1e7f53/attachment.html>
More information about the swift-users
mailing list