Thinking about Swift API design

Posted by d_priyag on Sun, 05 Jan 2020 18:20:36 +0100

Reference documents: Official Swift API design specification

  1. Explain why the first two methods declare the parameter tag at, and the second method defaults.
extension List {
  public mutating func remove(at position: Index) -> Element
  public mutating func remove(_ member: Element) -> Element?
}
  1. Which of the following two statements is correct? Explain why.
func add(_ observer: NSObject, for keyPath: String)
func addObserver(_ observer: NSObject, forKeyPath path: String)
  1. Which of the following statements is better? Explain why.
x.subViews(havingColor: y)
x.subViews(color: y)
  1. Which of the following statements is better? Explain why.
let foreground = Color(red: 32, green: 64, blue: 128)
let foreground = Color(havingRGBValuesRed: 32, green: 64, andBlue: 128)
  1. What are the differences between the following two methods?
x.sort()
x.sorted()
  1. What are the naming rules for Protocol?
  2. When can global functions be declared?
  3. What is the reason why the method declared below defaults to 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 { ... }
}
  1. What's the problem with this statement?
extension Box {
  /// Returns the `Int` stored in `self`, if any, and
  /// `nil` otherwise.
  func value() -> Int? { ... }

  /// Returns the `String` stored in `self`, if any, and
  /// `nil` otherwise.
  func value() -> String? { ... }
}
  1. Which of the following parameter names is better?
func filter(_ predicate: (Element) -> Bool) -> [Generator.Element]
func filter(_ includedInResult: (Element) -> Bool) -> [Generator.Element]
  1. Where is the error in the following statement?
extension String {
/// ...description...
    public func compare(_ options: CompareOptions = [], other: String, range: Range? = nil, locale: Locale? = nil
    ) -> Ordering
}
  1. Why don't the following functions need to declare parameter labels?
min(number1, number2)
zip(sequence1, sequence2)
  1. Why do the following two initialization methods have parameter labels and none?
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) 
}
  1. Which of the following two method declarations is better?
// A move to a point
a.move(toX: b, y: c)
a.moveTo(x: b, y: c)
  1. Which of the following two method declarations is better?
extension UIView {
    func addSubview(_ view: UIView)
    func add(subview: UIView)
}
  1. What questions might the following statement raise?
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
}
  1. Which of the following is correct:
struct Model {
    var sourceURL: URL
    var sourceUrl: URL
}

Comprehensive questions

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

Topics: Swift