Official Swift API design Specification

  1. Explain why the first method declares the parameter label at and the second method defaults.
extension List {
  public mutating func remove(at position: Index) -> Element
  public mutating func remove(_ member: Element) -> Element?
}
Copy the code
  1. Which of the following two statements is correct and explain why.
func add(_ observer: NSObject, for keyPath: String)
func addObserver(_ observer: NSObject, forKeyPath path: String)
Copy the code
  1. Which of the following statements is better, and why?
x.subViews(havingColor: y)
x.subViews(color: y)
Copy the code
  1. Which of the following statements is better, and why?
let foreground = Color(red: 32, green: 64, blue: 128)
let foreground = Color(havingRGBValuesRed: 32, green: 64, andBlue: 128)
Copy the code
  1. What is the difference between the following two methods?
x.sort()
x.sorted()
Copy the code
  1. ProtocolWhat are the naming rules?
  2. When can global functions be declared?
  3. Why does the method declared below default the first parameter label?
extension Shape {
  /// Returns `true` iff `other` is within the area of `self`.
  func contains(_ other: Point) -> Bool{... }/// Returns `true` iff `other` is entirely within the area of `self`.
  func contains(_ other: Shape) -> Bool{... }}Copy the code
  1. What is the problem with the following statement?
extension Box {
  /// Returns the `Int` stored in `self`, if any, and
  /// `nil` otherwise.
  func value(a) -> Int? {... }/// Returns the `String` stored in `self`, if any, and
  /// `nil` otherwise.
  func value(a) -> String? {... }}Copy the code
  1. Which of the following parameter names is better?
func filter(_ predicate: (Element) -> Bool) - > [Generator.Element]
func filter(_ includedInResult: (Element) -> Bool) - > [Generator.Element]
Copy the code
  1. Where is the error declared below?
extension String {
/ / /... description...
	public func compare(_ options: CompareOptions = [], other: String, range: Range? = nil, locale: Locale? = nil
	) -> Ordering
}
Copy the code
  1. Why don’t the following functions declare parameter labels?
min(number1, number2)
zip(sequence1, sequence2)
Copy the code
  1. Why does one of the following two initialization methods have a parameter label and one does not?
extension UInt32 {
/// Creates an instance having the specified value.
init(_ value: Int16)           
/// Creates an instance having the lowest 32 bits of source.
init(truncating source: UInt64)}Copy the code
  1. Which of the following method declarations is better?
// A moves to a certain point
a.move(toX: b, y: c)
a.moveTo(x: b, y: c)
Copy the code
  1. Which of the following method declarations is better?
extension UIView {
	func addSubview(_ view: UIView)
	func add(subview: UIView)
}
Copy the code
  1. What problems might arise from the following statement?
struct Array {
  /// Inserts `newElement` at `self.endIndex`.
  public mutating func append(_ newElement: Element)

  /// Inserts the contents of `newElements`, in order, at
  /// `self.endIndex`.
  public mutating func append(_ newElements: S)
    where S.Generator.Element= =Element
}
Copy the code
  1. Which of the following names is correct:
struct Model {
	var sourceURL: URL
	var sourceUrl: URL
}
Copy the code

Comprehensive problem

  • When is the default parameter label for the first argument to a method or function?