func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoStrings(_ a: inout String, _ b: inout String) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoDoubles(_ a: inout Double, _ b: inout Double) {
let temporaryA = a
a = b
b = temporaryA
}
<T>
λ‘ μ λ¬νλ κ²μ.func swapTwoValues<T>(_ a: inout T, _ b: inout T) {
let temporaryA = a
a = b
b = temporaryA
}
+) μ¬μ€ swap μ κ²½μ° μ΄λ―Έ μ νμμ μ 곡νλκΉ κ΅¬νν΄μ μ°μ§ λ§μ.
Dictionary<Key, Value>
, Array<Element>
μ²λΌ κΈ°λ³Έμ μΈ μ΄λ¦μ΄ μ ν΄μ Έ μλ κ²½μ°κ° μμ.T U V
κ°μ λ¨μΌ λ¬Έμλ₯Ό μ¬μ©ν΄μ μ§μ ν΄μ£Όλ κ²μ΄ μΌλ°μ .Array
Dictionary
μ²λΌ λͺ¨λ νμ
μμ λμν μ μλ μ¬μ©μ μ μ ν΄λμ€, ꡬ쑰체, μ΄κ±°νμμ) μ€ν ꡬν
// λ§μ½ Int λΌλ©΄ μ΄λ°μμΌλ‘ μ½λλ₯Ό 지거λ€.
struct IntStack {
var items: [Int] = []
mutating func push(_ item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
}
// μ€ν μμ μλ κ²μ΄ λͺ¨λ κ°μ νμ
μ΄κΈ°λ§ νλ©΄ λλκΉ
// ꡬ쑰체μ΄λ¦<νμ
>
struct Stack<Element> {
var items: [Element] = []
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
}
var stackOfStrings = Stack<String>()
stackOfStrings.push("uno")
stackOfStrings.push("dos")
stackOfStrings.push("tres")
stackOfStrings.push("cuatro")
// the stack now contains 4 strings
extension Stack {
var topItem: Element? {
return items.isEmpty ? nil : items[items.count - 1]
}
}
<T: SomeClass, U: SomeProtocol>
μ²λΌ μμμ νμ
μ΄ λ°λΌμΌ νλ κ²μ λ€μ μ¨μ£ΌκΈ°.func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
// function body goes here
}
// String λ°°μ΄μμ μ ννμ.
func findIndex(ofString valueToFind: String, in array: [String]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
==
μ΄ μ΄λ€ νμ
μμλ μ¬μ© κ°λ₯ν κ²μ΄ μλλ―λ‘ T λ Equatablefunc findIndex<T: Equatable>(of valueToFind: T, in array:[T]) -> Int? {
for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
// Item μ΄λΌλ μμμ νμ
.
// Item μ΄ λ¬΄μμΌμ§ λͺ¨λ₯΄μ§λ§ ν΄λΉ νμ
μ μ΄μ©ν΄μ νλ‘ν μ½μ μ μν μ μμ.
protocol Container {
associatedtype Item
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
// Container Type μ λ°λ₯΄λλ°, Item μ΄ Int λΌκ³ μ μν΄μ€ μ μλ°.
struct IntStack: Container {
// original IntStack implementation
var items: [Int] = []
mutating func push(_ item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
// conformance to the Container protocol
typealias Item = Int
mutating func append(_ item: Int) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Int {
return items[i]
}
}
// νΉμ μ ν΄μ Έμμ§ μμλ, Generic νκ² μ¬μ© κ°λ₯.
struct Stack<Element>: Container {
// original Stack<Element> implementation
var items: [Element] = []
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
// conformance to the Container protocol
mutating func append(_ item: Element) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> Element {
return items[i]
}
}
protocol Container {
associatedtype Item: Equatable
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
protocol SuffixableContainer: Container {
associatedtype Suffix: SuffixableContainer where Suffix.Item == Item
func suffix(_ size: Int) -> Suffix
}
extension Stack: SuffixableContainer {
func suffix(_ size: Int) -> Stack {
var result = Stack() // Stack λ Generic Type μ΄λκΉ, Stack<Int>() λ‘ μ νν μλ μμ.
for index in (count-size)..<count {
result.append(self[index])
}
return result
}
// Inferred that Suffix is Stack.
}
var stackOfInts = Stack<Int>()
stackOfInts.append(10)
stackOfInts.append(20)
stackOfInts.append(30)
let suffix = stackOfInts.suffix(2)
// suffix contains 20 and 30
<T: Equatable>
μ μ΄μ©ν΄μ Generic function, subscript, Type, parameter μ νμ
μ λν μꡬμ¬νμ μ μν μ μλ€.where
μ κ³Ό ν¨κ»λΌλ©΄~// 컨ν
μ΄λ 2κ°κ° κ°μ μμλ‘ μμ΄ν
μ κ°μ§κ³ μλμ§ νμΈνλ ν¨μ.
// C1, C2 κ° μ»¨ν
μ΄λμ΄λ κ°μ νμ
μ μλμλ μλ μν©.
func allItemsMatch<C1: Container, C2: Container>
(_ someContainer: C1, _ anotherContainer: C2) -> Bool
where C1.Item == C2.Item, C1.Item: Equatable {
// Check that both containers contain the same number of items.
if someContainer.count != anotherContainer.count {
return false
}
// Check each pair of items to see if they're equivalent.
for i in 0..<someContainer.count {
if someContainer[i] != anotherContainer[i] {
return false
}
}
// All items match, so return true.
return true
}
// Stack μ Element κ° Equatable ν λλ§ μ΄ λ©μλ μΆκ°.
extension Stack where Element: Equatable {
func isTop(_ item: Element) -> Bool {
guard let topItem = items.last else {
return false
}
return topItem == item
}
}
// Item μ΄ Double μΌλλ§ λ©μλ μΆκ°.
extension Container where Item == Double {
func average() -> Double {
var sum = 0.0
for index in 0..<count {
sum += self[index]
}
return sum / Double(count)
}
}
print([1260.0, 1200.0, 98.6, 37.0].average()) // Prints "648.9"
extension Container {
// Int μΌ λ μ¬μ© κ°λ₯ν λ©μλ.
func average() -> Double where Item == Int {
var sum = 0.0
for index in 0..<count {
sum += Double(self[index])
}
return sum / Double(count)
}
// Equatable μΌ λ μ¬μ© κ°λ₯ν λ©μλ
func endsWith(_ item: Item) -> Bool where Item: Equatable {
return count >= 1 && self[count-1] == item
}
}
let numbers = [1260, 1200, 98, 37]
print(numbers.average()) // Prints "648.75"
print(numbers.endsWith(37)) // Prints "true"
protocol Container {
associatedtype Item
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
// Iterator.Element κ° Item μΌ λ μ¬μ© κ°λ₯ν νμ
Iterator
associatedtype Iterator: IteratorProtocol where Iterator.Element == Item
func makeIterator() -> Iterator
}
protocol ComparableContainer: Container where Item: Comparable { }
extension Container {
subscript<Indices: Sequence>(indices: Indices) -> [Item]
// λ°λ³΅νλ κ°μ΄ Int μΌλλ§ κ°λ₯νκ².
where Indices.Iterator.Element == Int {
var result: [Item] = []
for index in indices {
result.append(self[index])
}
return result
}
}
<T>