By Erica Sadun, Translator: Zhao Lei; Proofread by Chime; Finalized: Cee

Many function calls in Cocoa and Cocoa Touch are very rigid. Almost every call takes common parameter values. Why not use the Swift default method instead? We can simplify this function:


dismissViewControllerAnimated(flag: true, completion: nil)

You can call it like this:


dismiss()

The default value

SE-0005 introduces a series of methods for the OBJC API to obtain default values through automatic translation. Under this proposal, any method that conforms to a certain pattern can provide a default value.

Daniel Steinberg and I both like this default behavior, and we also went through the Swift evolution list to see if we could add more defaults to the Cocoa and Cocoa Touch APIs. David Abrahams replied that it would be better to put the specific requests together and submit them to the UIKit/AppKit team via the Apple radar system, and they might be able to provide extensions that pass the default values.

Here is an updated preliminary list. We want you to join us to help expand this list, so that when we submit a bug to Apple we think it through and the changes are prioritized.

I’ve given you a rundown, and if you’re interested in contributing you can contact us in the comments, on Twitter, or by email.

Controls

We think that the state of UIControl should default to.normal. Compare these two calls: Button.setTitle (“Hello”) and Button.setTitle (“Hello”, forState:.normal). Because these are just default values, you can always override the state of the control by completing specific parameters (such as changing to highlight).

The actions of controlling elements are also very predictable. Buttons are usually triggered by.TouchUpInside, slider and switch are. ValueChanged. By providing default values that can only be completed if the developer intends to use non-standard template methods, the amount of code can be greatly simplified.

Core Graphics

We want to initialize all CG constructors to 0.0 for the x, y, dX, dY, width, and height properties. There is no limit to this, you can either specify all the parameters, or you can initialize a rectangle based on some default values, such as cGRect (.width: 200,.height: 100).

Views

We’re going to initialize animated to true in most UIKit functions. Instead of saying super.viewWillAppear() — it should continue passing in the animation parameters passed to you. We mean instead of similar dismissViewControllerAnimated function, most of the UIKit (and possibly the Cocoa) scenario use animation effects. Adding a default value here simplifies the invocation in an easily understood “common practice” approach.

Auto Layout

NSLayoutConstraint has a large historical burden to deal with. But almost everyone agreed that Multiplier would be set to 1.0 by default and Constant to 0.0.

However, my initial suggestion would have the default relationship (relatedBy:,.equal), the second content (toItem:, nil), and the second attribute (attribute:,.notanAttribute) have hidden arguments with default values, causing readability problems. Especially in general, multiple views are laid out relative to each other, not a single view attribute (NSLayOutConstraint (item: view, attribute:.width, constant: 400.0)), we haven’t agreed on the second part of this default mode.

Other common patterns

Apple’s UIKit Framework Reference further provides an overview of UIKit classes, extensions, functions, constants, and data types. I’m sure there’s something in Cocoa and this reference library that corresponds. If you want to add something, you can help us find examples where developers use APIs that have obvious default values, but have to be completed on every call, and are not yet indexed to SE-0005.

If you find examples of this, contact any of us and we’ll update your feedback to the list. Our goal is to strive to expand this list and submit it to Apple after rigorous review.

Thanks in advance!

This article is translated by Swiftgg Translation Group and has been translated with authorizations. Please visit http://swift.gg for the latest article.