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
> <[email protected]> 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
>> <[email protected] <mailto:[email protected]>> 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 <[email protected]
>>> <mailto:[email protected]>> 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
>>>> <[email protected] <mailto:[email protected]>> 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
>>>> [email protected] <mailto:[email protected]>
>>>> https://lists.swift.org/mailman/listinfo/swift-users
>>>> <https://lists.swift.org/mailman/listinfo/swift-users>
>>>
>>
>> _______________________________________________
>> swift-users mailing list
>> [email protected] <mailto:[email protected]>
>> https://lists.swift.org/mailman/listinfo/swift-users
>
> _______________________________________________
> swift-users mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users