Reference documents: Official Swift API design specification
- 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? }
- Which of the following two statements is correct? Explain why.
func add(_ observer: NSObject, for keyPath: String) func addObserver(_ observer: NSObject, forKeyPath path: String)
- Which of the following statements is better? Explain why.
x.subViews(havingColor: y) x.subViews(color: y)
- 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)
- What are the differences between the following two methods?
x.sort() x.sorted()
- What are the naming rules for Protocol?
- When can global functions be declared?
- 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 { ... } }
- 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? { ... } }
- Which of the following parameter names is better?
func filter(_ predicate: (Element) -> Bool) -> [Generator.Element] func filter(_ includedInResult: (Element) -> Bool) -> [Generator.Element]
- 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 }
- Why don't the following functions need to declare parameter labels?
min(number1, number2) zip(sequence1, sequence2)
- 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) }
- 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)
- Which of the following two method declarations is better?
extension UIView { func addSubview(_ view: UIView) func add(subview: UIView) }
- 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 }
- 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?