Swift and ObjC InterOp: Implicit Unwrapped Optionals vs. Non-Optionals when using NSParameterAssert()

Originator:evan
Number:rdar://17235907 Date Originated:09-Jun-2014 02:39 PM
Status:Duplicate Resolved:10-Jun-2014 07:03 PM
Product:Developer Tools Product Version:
Classification:Enhancement Reproducible:N/A
 
Summary:
Given how smart the compiler is at helping interoperability when using ObjC API from Swift, I thought I'd suggest the following enhancement.

When writing my own API, part of those contracts is that `nil` is sometimes disallowed as a value for an argument. Apple has encouraged enforcing this by using the NSParameterAssert macro in the first lines inside a method's implementation.

Could it be possible that the compiler can see these assertions, and therefore mark a parameter as non-optional?

Steps to Reproduce:
__Example from NSFileManager Documentation__

Copies the item at the specified path to a new location synchronously.

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

Parameters

**srcPath**
The path to the file or directory you want to move. This parameter must not be nil.

**dstPath**
The path at which to place the copy of srcPath. This path must include the name of the file or directory in its new location. This parameter must not be nil.

**error**
On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

------------

When converted to Swift, the method signature looks like this:

func copyItemAtPath(_ srcPath: String!, toPath dstPath: String!, error error: NSErrorPointer) -> Bool

------------

Of course I'm making an assumption to drive my point, but in the implementation of this method, there should be:

NSParameterAssert(srcPath);
NSParameterAssert(dstPath);

...which could allow Swift to generate a method signature like this:

func copyItemAtPath(_ srcPath: String, toPath dstPath: String, error error: NSErrorPointer) -> Bool

(note lack of exclamation points)

=====================================================

Apple Developer Relations10-Jun-2014 07:03 PM

Engineering has determined that your bug report is a duplicate of another issue and will be closed. 

The open or closed status of the original bug report your issue was duplicated to appears in the yellow "Duplicate of XXXXXXXX" section of the bug reporter user interface. This section appears near the top of the right column's bug detail view just under the bug number, title, state, product and rank.

If you have any questions or concerns, please update your report directly here: http://bugreport.apple.com/.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!