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