4 ms·
This might be easily overlooked: optional has been introduced (back-ported?) to Objective-C in the form of nullability annotation!
by DaNmarner 12y ago
This might be easily overlooked: optional has been introduced (back-ported?) to Objective-C in the form of nullability annotation!
- jPaolantonio 12y agoI think this is a much-needed feature for integrating Swift into existing large Objective-C apps. Dealing with all the AnyObject optionals coming from Cocoa frameworks makes working with Swift painful.
- oleganza 12y agoNot exactly. ObjC code cannot usefully benefit from those annotations (yet? more likely ever). At most, it's good for documenting your ObjC code for fellow readers. The actual use appears in the Swiftland where instead of implicitly unwrapped optionals you get proper explicit optionals or explicit non-optionals.
- ejdyksen 12y agoThe benefit to working with the Cocoa APIs will be immense, though. I assume that, now that this feature exists, Apple will begin annotating their Obj-C APIs.
- oleganza 12y agoThey already started doing it a few months ago with private annotations. This "nonnull"/"nullable" syntax is a result of their work on annotating Cocoa APIs.
- eridius 12y agoThey've been doing it for a while in the Swift overlay module. I'm really happy that this is officially added to obj-c because it provides very valuable API documentation, even if it won't have much practical effect on the code.
- austinz 12y agoIt looks like the annotations will allow the compiler to produce warnings when compiling the Objective-C code, so that's something: "However, nullability annotations—in addition to improving the experience in Swift—provide new warnings in Objective-C"
- frr149 12y agoThis is quite horrible. I wish they would leave Objective C alone.
- eridius 12y agoNo it hasn't. Optional is a Swift type that contains a value of any type. Obj-C doesn't have anything matching that behavior at all. What's been done here is Obj-C has gained the ability to annotate any pointer valued type as either nullable or non-nullable. This does not affect the runtime behavior of the code in the slightest. It doesn't even affect the semantics of Obj-C. All it does for Obj-C is let the compiler yell at you if you pass nil to a non-nullable parameter / property. The real point of these annotations is so Swift can be more intelligent when translating the obj-c API to Swift, choosing to use the correct optional type (or no optional type at all).