Parameters

๋ฌธ์„œ์— ์ œ๊ณตํ• ๋งŒํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์„ ํƒํ•˜์ž.

  • ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์„ ์ˆจ๊ฒจ ๋ณด์ด์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋„ ์„ค๋ช…์— ์žˆ์–ด์„œ๋Š” ์ค‘์š”ํ•˜๋‹ค.
/// Return an `Array` containing the elements of `self`
/// that satisfy `predicate`.
func filter(_ predicate: (Element) -> Bool) -> [Generator.Element]
 
/// Replace the given `subRange` of elements with `newElements`.
mutating func replaceRange(_ subRange: Range, with newElements: [E]) // subrange

์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ Default Parameter๋ฅผ ์‚ฌ์šฉํ•ด๋ผ.

  • ์ผ๋‹จ ์ธ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ฉด ์ง€์ €๋ถ„ํ•˜๋‹ค.
  • ๋งŒ์•ฝ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ์— ์ธ์ž๊ฐ€ ํ•˜๋‚˜๋งŒ ๋“ค์–ด๊ฐ€๊ณ , ๊ธฐ๋ณธ๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค๋ฉด Default Parameter๋ฅผ ์‚ฌ์šฉํ•ด๋ผ.
// Bad
let order = lastName.compare(
  royalFamilyName, options: [], range: nil, locale: nil)
// Good
let order = lastName.compare(royalFamilyName)

๊ธฐ๋ณธ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก์˜ ๋์— ์œ„์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

  • ์ฆ‰, ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฒฝ์šฐ ์•ž์—, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ๋’ค์ชฝ์œผ๋กœ ๋‘๋ผ๋Š” ๋ง์ด๋‹ค.
  • ์ดํ•ด๊ฐ€ ๋ณด๋‹ค ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Argument Labels

์ธ์ˆ˜๋ฅผ ์œ ์šฉํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด โ€œ์ƒ๋žตโ€ํ•ด๋ผ.

min(number1, number2)
zip(sequence1, sequence2)

๊ฐ’์€ ๊ฐ™์€๋ฐ ํƒ€์ž…๋งŒ ๋ณ€๊ฒฝํ•  ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ƒ์„ฑ์ž์—์„œ๋Š” ์ฒซ๋ฒˆ์งธ Argument Label์„ ์ƒ๋žตํ•ด๋ผ.

Int64(someUInt32)
Int(someString)
extension String {
  // Convert `x` into its textual representation in the given radix
  init(_ x: BigInt, radix: Int = 10)   // Note the initial underscore
}
 
extension UInt32 {
  init(_ value: Int16)           // ๋‹จ์ˆœ ๋ณ€ํ™˜, label์ด ์—†๋‹ค.
  
  init(truncating source: UInt64) // ๊ฐ’์„ ์ž๋ฅธ๋‹ค๋Š” ์ถ”๊ฐ€ ๋™์ž‘์ด ๋“ค์–ด๊ฐ„๋‹ค. label์ด ์žˆ๋‹ค.
 
  init(saturating valueToApproximate: UInt64) // ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ •์ˆ˜๋กœ ๋–จ์–ดํŠธ๋ฆฌ๋Š” ๋™์ž‘์ด ์žˆ๋‹ค.
}

์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ ์ „์น˜์‚ฌ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋ฉด Argument Label์— ์‚ฌ์šฉํ•ด๋ผ.

x.removeBoxes(havingLength: 12)
 
removeBoxes(havingLength length: Int) // ์ด๋Ÿฐ์‹์œผ๋กœ ๋˜์–ด ์žˆ์„ ๊ฒƒ.
  • ์˜ˆ์™ธ ์ƒํ™ฉ์ด ์žˆ๋‹ค.
  • ๋‘๊ฐœ ์ด์ƒ์˜ arguments๊ฐ€ ๋™์ผํ•œ ์ถ”์ƒํ™”์ •๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, arguement label์„ ์ „์น˜์‚ฌ ๋‹ค์Œ์— ์‹œ์ž‘ํ•˜๋„๋ก ํ•˜์ž.
  • ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ณด๋‹ค ๋ช…ํ™•ํ•˜๊ฒŒ ์ฝํžˆ๊ฒŒ ๋œ๋‹ค.
// Bad
a.move(toX: b, y: c)
a.fade(fromRed: b, green: c, blue: d)
a.moveTo(x: b, y: c)
a.fadeFrom(red: b, green: c, blue: d)

์ฒซ๋ฒˆ์งธ Arguments๊ฐ€ ์ „์น˜์‚ฌ๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, label์„ ์ƒ๋žตํ•ด๋ผ.

x.addSubview(y) // ๊ตณ์ด subviews๋ผ๋Š” label์„ ๋ถ™์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ๊ทธ๋Ÿฐ๋ฐ ํ•จ์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๋ณด๋‹ˆ๊นŒ, ๋ฌธ๋ฒ•์ ์œผ๋กœ ์˜ณ์€ ๊ตฌ๋ฅผ ๋งŒ๋“ค๊ณ  ์žˆ์ง€ ์•Š๋‹ค๋ฉด label์„ ๋ถ™์—ฌ๋ผ.
// Bad
view.dismiss(false)   // Don't dismiss? Dismiss a Bool?
words.split(12)       // Split the number 12?
// Good
view.dismiss(animated: false)
let text = words.split(maxSplits: 12)
let studentsByName = students.sorted(isOrderedBefore: Student.namePrecedes)

๋‹ค๋ฅธ ๋ชจ๋“  ์ธ์ˆ˜์— label์„ ๋‹ฌ์•„๋ผ.

  • ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ฒซ๋ฒˆ์งธ์— ๋Œ€ํ•ด์„œ๋งŒ ์–˜๊ธฐํ–ˆ๋‹ค.
  • ๋‘๋ฒˆ์งธ, ์„ธ๋ฒˆ์งธ argument์—๋Š” ๋ชจ๋‘ label์„ ๋‹ฌ์œผ๋ผ๋Š” ๋ง์ด๋‹ค.
  • ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ๋ณธ์ ์ด ์žˆ๋Š”๋ฐ, ์ง„์งœ ์–ด๋–ค ํ•จ์ˆ˜์ธ์ง€ ๊ฐ์ด ์•ˆ์˜จ๋‹ค.

Special Instructions

ํŠœํ”Œ ๋ฉค๋ฒ„์— ์ด๋ฆ„์„ ๋‹ฌ์•„๋ผ.

mutating func ensureUniqueStorage(minimumCapacity requestedCapacity: Int, allocate: (_ byteCount: Int) -> UnsafePointer<Void>) -> (reallocated: Bool, capacityChanged: Bool)

Any, AnyObject์™€ ๊ฐ™์€ ์ œ์•ฝ ์—†๋Š” ๋‹คํ˜•์„ฑ ์‚ฌ์šฉํ•˜๋Š” ํƒ€์ž…๋“ค์„ ์กฐ์‹ฌํ•ด๋ผ. ๋ชจํ˜ธํ•จ์ด ์ฆ๊ฐ€๋  ์ˆ˜ ์žˆ๋‹ค.

struct Array {
  // ๊ฐ€์žฅ ๋’ค์— element๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
  public mutating func append(_ newElement: Element)
 
  // ๊ฐ€์žฅ ๋’ค์— elements๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ๋Š”๋‹ค. ์–˜๋Š” ์—ฌ๋Ÿฌ๊ฐœ๋‹ค.
  public mutating func append(_ newElements: S)
    where S.Generator.Element == Element
}
  • ์ด๋ ‡๊ฒŒ๋งŒ ๋ณด๋ฉด ํฐ ๋ฌธ์ œ๊ฐ€ ์—†์–ด๋ณด์ธ๋‹ค.
  • ๋ณดํ†ต ๊ฐ™์€ ์›์†Œ๊ฐ€ ๋“ค์–ด์˜ฌ ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์—.
  • ๊ทธ๋Ÿฐ๋ฐ Element๊ฐ€ Any, AnyObject์™€ ๊ฐ™์€ ํƒ€์ž…์ด๋ผ๋ฉด ์–ด๋–จ๊นŒ?
var values: [Any] = [1, "a"]
values.append([2, 3, 4]) // [1, "a", [2, 3, 4]] or [1, "a", 2, 3, 4]?
  • ๊ฐ‘์ž๊ธฐ ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ๋“ค์–ด๊ฐ€๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
struct Array {
 
  public mutating func append(_ newElement: Element)
 
  public mutating func append(contentsOf newElements: S)
    where S.Generator.Element == Element
}

์‹ค์ „์— ์‚ฌ์šฉํ•  ๋งŒํ•œ TIP

// ์ด๋Ÿฐ์‹์œผ๋กœ Range๋ฅผ ํ•จ์ˆ˜์ด๋ฆ„์— ๋ถ™์ด๊ณ , ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์„ ์ˆจ๊ธด๋‹ค.
// ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฐ›๋Š” ์ธ์ž์— ๋Œ€ํ•ด ์ „์น˜์‚ฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
mutating func replaceRange(_ r: Range, with: [E]) 
 
a.moveTo(x: b, y: c)
a.fadeFrom(red: b, green: c, blue: d)
 
x.removeBoxes(havingLength: 12)
removeBoxes(havingLength length: Int) // ์ด๋Ÿฐ์‹์œผ๋กœ ๋˜์–ด ์žˆ์„ ๊ฒƒ.

Reference