[swift-users] Questions for bridging from OC to Swift

old donkey olddonkeyblog at gmail.com
Tue May 30 14:11:34 CDT 2017


Hi All:

Current bridging from Objective-C to Swift has this behaviour:

If we define a property in Objective-C, and we don’t say it is nullable or
nonnull, like this: @property (nonatomic, strong, readonly) NSString
*someString;


When we reference it from Swift, this property will be bridging to* String!*
(*implicitly unwrapped optional*) type automatically, and *there is no
warning indicate we should check if this is nil first.*


So the result is when some edge cases happen, the App will crash.


As more and more large projects are trying to use Swift now, this may seems
a problem for more people.


The documents I can find is: https://developer.apple.com/swift/blog/?id=25
 and https://developer.apple.com/library/content/documentation/
Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html, it
says “Swift cannot distinguish between optional and nonoptional references,
and imports it as an implicitly unwrapped optional.”


As in Objective-C, the default property is nullable(if you don’t say it is
nonnull), I am curious what’s the reason that the property is not bridging
to optional, for me, feel optional value should be a better fit here.


Anyone know what’s the consideration at Swift 2 when this change happens?

Regards,
olddonkey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170530/80e8470e/attachment.html>


More information about the swift-users mailing list