# Decimal imported as NSDecimal not NSDecimalNumber in Swift 3 to Objective C

**URL:** <https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498>\
**Category:** Using Swift\
**Created:** [November 11, 2016, 9:46pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498 "2016-11-11T21:46:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisanderson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chrisanderson/32/1377_2.png) [@chrisanderson](https://forums.swift.org/u/chrisanderson)\
**Post date:** [November 11, 2016, 9:46pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/1 "2016-11-11T21:46:21Z")

</div>

I'm having problems with the type conversion between a Swift `Decimal` and  
an Objective C `NSDecimalNumber`.

If I have the Swift class:

&nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
&nbsp;&nbsp;&nbsp;&nbsp;}

And try to use that Swift class in Objective C,

&nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
&nbsp;&nbsp;&nbsp;&nbsp;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  
[Apple Developer Documentation](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

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [November 11, 2016, 10:55pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/2 "2016-11-11T22:55:13Z")

</div>

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@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:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > And try to use that Swift class in Objective C,
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
> > &nbsp;&nbsp;&nbsp;&nbsp;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 [NSDecimalNumber | Apple Developer Documentation](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@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![chrisanderson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chrisanderson/32/1377_2.png) [@chrisanderson](https://forums.swift.org/u/chrisanderson)\
**Post date:** [November 11, 2016, 10:56pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/3 "2016-11-11T22:56:20Z")

</div>

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@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@swift.org \<mailto:swift-users@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:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}
> > > 
> > > And try to use that Swift class in Objective C,
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
> > > &nbsp;&nbsp;&nbsp;&nbsp;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 [NSDecimalNumber | Apple Developer Documentation](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@swift.org \<mailto:swift-users@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![lickel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lickel/32/612_2.png) [@lickel](https://forums.swift.org/u/lickel)\
**Post date:** [November 11, 2016, 10:58pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/4 "2016-11-11T22:58:27Z")

</div>

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@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@apple.com \<mailto:anthony.parker@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@swift.org \<mailto:swift-users@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:
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;}
> > > > 
> > > > And try to use that Swift class in Objective C,
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;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 [NSDecimalNumber | Apple Developer Documentation](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@swift.org \<mailto:swift-users@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [November 11, 2016, 11:07pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/5 "2016-11-11T23:07:19Z")

</div>

NSDecimal is not toll free bridged, but it does have a bridge to NSDecimalNumber.

So take this for example:

@objc class Exam: NSObject {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 {  
&nbsp;&nbsp;var grade : Decimal  
}

> **···**
>
> > On Nov 11, 2016, at 2:58 PM, Adam C. Lickel via swift-users \<swift-users@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@swift.org \<mailto:swift-users@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@apple.com \<mailto:anthony.parker@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@swift.org \<mailto:swift-users@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:
> > > > > 
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;}
> > > > > 
> > > > > And try to use that Swift class in Objective C,
> > > > > 
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;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 [NSDecimalNumber | Apple Developer Documentation](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@swift.org \<mailto:swift-users@swift.org\>  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-users mailing list  
> > > swift-users@swift.org \<mailto:swift-users@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![chrisanderson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chrisanderson/32/1377_2.png) [@chrisanderson](https://forums.swift.org/u/chrisanderson)\
**Post date:** [November 14, 2016, 9:59pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/6 "2016-11-14T21:59:31Z")

</div>

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.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let value = NSDecimalNumber(value: 2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let test = 2.0 // double  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let product = value.multiplying(by: 2.0) // compiles  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let x = value.multiplying(by: Decimal(2.0)) // does not compile  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let y = value.multiplying(by: Double(2.0)) // does not compile  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let z = value.multiplying(by: test) // does not compile

Best,  
Chris Anderson

> **···**
>
> > On Nov 11, 2016, at 6:07 PM, Philippe Hausler \<phausler@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 {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 {  
> > &nbsp;&nbsp;var grade : Decimal  
> > }
> > 
> > > On Nov 11, 2016, at 2:58 PM, Adam C. Lickel via swift-users \<swift-users@swift.org \<mailto:swift-users@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@swift.org \<mailto:swift-users@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@apple.com \<mailto:anthony.parker@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@swift.org \<mailto:swift-users@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:
> > > > > > 
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;}
> > > > > > 
> > > > > > And try to use that Swift class in Objective C,
> > > > > > 
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;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 [NSDecimalNumber | Apple Developer Documentation](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@swift.org \<mailto:swift-users@swift.org\>  
> > > > > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-users mailing list  
> > > > swift-users@swift.org \<mailto:swift-users@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-users mailing list  
> > > swift-users@swift.org \<mailto:swift-users@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [November 14, 2016, 11:07pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/7 "2016-11-14T23:07:19Z")

</div>

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@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.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let value = NSDecimalNumber(value: 2)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let test = 2.0 // double  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let product = value.multiplying(by: 2.0) // compiles  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let x = value.multiplying(by: Decimal(2.0)) // does not compile  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let y = value.multiplying(by: Double(2.0)) // does not compile  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let z = value.multiplying(by: test) // does not compile
> > 
> > Best,  
> > Chris Anderson
> > 
> > > On Nov 11, 2016, at 6:07 PM, Philippe Hausler \<phausler@apple.com \<mailto:phausler@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 {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 {  
> > > &nbsp;&nbsp;var grade : Decimal  
> > > }
> > > 
> > > > On Nov 11, 2016, at 2:58 PM, Adam C. Lickel via swift-users \<swift-users@swift.org \<mailto:swift-users@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@swift.org \<mailto:swift-users@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@apple.com \<mailto:anthony.parker@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@swift.org \<mailto:swift-users@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:
> > > > > > > 
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;}
> > > > > > > 
> > > > > > > And try to use that Swift class in Objective C,
> > > > > > > 
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
> > > > > > > &nbsp;&nbsp;&nbsp;&nbsp;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 [NSDecimalNumber | Apple Developer Documentation](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@swift.org \<mailto:swift-users@swift.org\>  
> > > > > > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-users mailing list  
> > > > > swift-users@swift.org \<mailto:swift-users@swift.org\>  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-users mailing list  
> > > > swift-users@swift.org \<mailto:swift-users@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [November 14, 2016, 11:02pm UTC](https://forums.swift.org/t/decimal-imported-as-nsdecimal-not-nsdecimalnumber-in-swift-3-to-objective-c/4498/8 "2016-11-14T23:02:42Z")

</div>

Literals in Swift do not inherently have a type (at least conceptually).  
They are simply literals.

The compiler will interpret them as whatever ExpressibleBy\_\_\_\_\_Literal type  
is required to make the expression sensible.

There are default types which literals will become if no type information  
is available: a string literal becomes String, a floating point literal  
becomes Double, and so forth. But the literals themselves do not have a  
type, and will become whatever type makes sense in context.

Nevin

> **···**
>
> On Mon, Nov 14, 2016 at 4:59 PM, Chris Anderson via swift-users \< swift-users@swift.org\> 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.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let value = NSDecimalNumber(value: 2)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let test = 2.0 //  
> > double  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let product = value.multiplying(by: 2.0) // compiles  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let x = value.multiplying(by: Decimal(2.0)) // does not compile  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let y = value.multiplying(by: Double(2.0)) // does not compile  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let z = value.multiplying(by: test) // does not  
> > compile
> > 
> > Best,  
> > Chris Anderson
> > 
> > On Nov 11, 2016, at 6:07 PM, Philippe Hausler \<phausler@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 {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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@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@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@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@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:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@objc class Exam: NSObject {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var grade: Decimal = 90.0  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > And try to use that Swift class in Objective C,
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Exam \*exam = [[Exam alloc] init];  
> > &nbsp;&nbsp;&nbsp;&nbsp;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  
> > [NSDecimalNumber | Apple Developer Documentation](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@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
