Improve Cocoa/CocoaTouch APIs from Swift

Originator:david.w.hart
Number:rdar://17189142 Date Originated:05-Jun-2014 05:27 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift seed 1
Classification:Enhancement Reproducible:Always
 
When accessing Cocoa and CocoaTouch APIs from swift, it would be nice to able to take advance of some of Swift features. For example, let's take one API that was discussed in the WWDC session: Building Modern Frameworks.

- (void)presentPopoverFromRect:(CGRect)rect
                        inView:(UIView *)view
      permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
                      animated:(BOOL)animated

The current signature of this API in Swift is:

func presentPopoverFromRect(_ rect: CGRect,
                     inView view: UIView!,
   permittedArrowDirections arrowDirections: UIPopoverArrowDirection,
                   animated animated: Bool)

Resulting in a call like this:

presentPopoverFromRect(myRect, inView: myView, permittedArrowDirections: .Any, animated: true)

Like mentioned in the session, it would be great if the signature of the Swift API could be rewritten to look like this:

func presentPopover(fromRect rect: CGRect,
                     inView view: UIView!,
   arrowDirections: UIPopoverArrowDirection = .Any,
                   animated: Bool = true)

Resulting in a call like this:

presentPopover(fromRect: myRect, inView: myView)

This improvement comes form two things:

1- Currently, the first parameter name is often part of the function name. But in Swift, it looks more natural to have it part of the first parameter name. APIs like UIView's

insertSubview(_ view: UIView!, atIndex index: Int)

could become,

insert(subview view: UIView!, atIndex index: Int)

2- Some APIs can also become much better by using default values.

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!