<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">NSDecimal is not toll free bridged, but it does have a bridge to NSDecimalNumber.</div><div class=""><br class=""></div><div class="">So take this for example:</div><div class=""><br class=""></div><div class="">@objc class Exam: NSObject {<br class=""> var grade: Double = 90.0<br class="">}</div><div class=""><br class=""></div><div class="">It would be reasonable to expect that is exposed in objc as:</div><div class=""><br class=""></div><div class="">@interface Exam : NSObject</div><div class="">@property double grade;</div><div class="">@end</div><div class=""><br class=""></div><div class="">and not:</div><div class=""><br class=""></div><div class="">@interface Exam : NSObject</div><div class="">@property NSNumber *grade;</div><div class="">@end</div><div class=""><br class=""></div><div class="">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:</div><div class=""><br class=""></div><div class="">@interface Exam : NSObject</div><div class="">@property NSDecimalNumber *grade;</div><div class="">@end</div><div class=""><br class=""></div><div class="">that should be imported into swift as:</div><div class=""><br class=""></div><div class="">class Exam : NSObject {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var grade : Decimal</div><div class="">}</div><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 11, 2016, at 2:58 PM, Adam C. Lickel via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">NSDecimal has toll-free bridging with NSDecimalNumber so you can still do as casting when talking to an Objective-C API.</div><div class=""><div class=""><div class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 11, 2016, at 2:56 PM, Chris Anderson via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Sure thing. Yeah, ideally the bridging would be fixed, but at the least, correcting the documentation will be a good start. Will file, thanks.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Chris Anderson</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On Nov 11, 2016, at 5:55 PM, Tony Parker <<a href="mailto:anthony.parker@apple.com" class="">anthony.parker@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Chris,<div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">- Tony</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 11, 2016, at 1:46 PM, Chris Anderson via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div style="font-family: helvetica; font-size: 12px;" class="">I'm having problems with the type conversion between a Swift `Decimal` and an Objective C `NSDecimalNumber`.</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">If I have the Swift class:</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class=""> @objc class Exam: NSObject {</div><div style="font-family: helvetica; font-size: 12px;" class=""> var grade: Decimal = 90.0</div><div style="font-family: helvetica; font-size: 12px;" class=""> }</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">And try to use that Swift class in Objective C, </div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class=""> Exam *exam = [[Exam alloc] init];</div><div style="font-family: helvetica; font-size: 12px;" class=""> NSDecimalNumber *result = [[NSDecimalNumber zero] decimalNumberByAdding:grade.value];</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">I get the error:</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">Sending 'NSDecimal' to parameter of incompatible type 'NSDecimalNumber * _Nonnull'</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">as it seems like `grade` is being treated as an `NSDecimal` not an `NSDecimalNumber`. This seems incorrect as per <a href="https://developer.apple.com/reference/foundation/nsdecimalnumber" class="">https://developer.apple.com/reference/foundation/nsdecimalnumber</a> it says </div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">"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."</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">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.</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">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.</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">Thanks for the help!</div><div style="font-family: helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: helvetica; font-size: 12px;" class="">Best,</div><div style="font-family: helvetica; font-size: 12px;" class="">Chris Anderson</div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>