์ฐธ๊ณ ์ฌ์ดํธ:
The swift Language Guide
Apple Developer
boostcourse
var examInt: Array<Int> = Array<Int>()
// ์ ์ธ์ ์ถ์ฝํ
var examInt: [Int] = []
var examInt: [Int] = [Int]()
examInt.append(3)
// insert value 3
examInt = []
// array clear
var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles : Double ํ์
์ [0.0, 0.0, 0.0]
์ด๊ฒ์ ๋ฐฐ์ด๋ง๊ณ ๋ String์์๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
let cake: String = String(repeating: "์น์ฆ์ผ์ดํฌ",count: 3)
>>> "์น์ฆ์ผ์ดํฌ์น์ฆ์ผ์ดํฌ์น์ฆ์ผ์ดํฌ"
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles : [2.5, 2.5, 2.5]
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles : [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
[value1, value2, value3] ํํ๋ฅผ ์ด์ฉํด ๋ฐฐ์ด์ ์์ฑํ ์ ์๋ค.
var shoppingList: [String] = ["Eggs", "Milk"]
var shoppingList = ["Eggs", "Milk"]
shoppingList.append("Meat)
// ๋ฆฌํฐ๋์ ์ด์ฉํ ๋ฐฐ์ด ์์ฑ๋ ๊ฐ ์ถ๊ฐ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค.
print("The shopping list contains \(shoppingList.count) items.")
// The shopping list contains 3 items.
if shoppingList.isEmpty{
print("I don't know shopping list")
} else{
print("I know \(shoppingList.count) shopping list")
}
// prints "I know 3 shopping list"
var students = ["Ben", "Ivy", "Jordell"]
students.append("Maxime")
// ["Ben", "Ivy", "Jordell", "Maxime"]
students.append(contentsOf: ["Shakia", "William"])
// ["Ben", "Ivy", "Jordell", "Maxime", "Shakia", "William"]
students += ["Jack","Steven"]
// ["Ben", "Ivy", "Jordell", "Maxime", "Shakia", "William", "Jack","Steven"]
students.insert("Liam", at: 3)
// ["Ben", "Ivy", "Jordell", "Liam", "Maxime", "Shakia", "William", "Jack", "Steven"]
students.remove(at: 0)
//students.removeFirst() // ๋์ผํ ํํ
// ["Ivy", "Jordell", "Liam", "Maxime", "Shakia", "William", "Jack", "Steven"]
students.removeLast()
// ["Ivy", "Jordell", "Liam", "Maxime", "Shakia", "William", "Jack"]
// ์ญ์ ํ ์ธ๋ฑ์ค ๋ฒ์๋ฅผ ์ง์ ํ ์ ์๋ค -> (1,2) ์ธ๋ฑ์ค ์์น์ ์๋ ๊ฐ ์ ๊ฑฐ
students.removeSubrange(1..<3)
// ["Ivy", "Maxime", "Shakia", "William", "Jack"]
if let i = students.firstIndex(of: "William"){
students[i] = "Will"
}
// ["Maxime", "Will", "Jack"]
students[1] = "Jujo"
// "Maxime", "Jujo", "Jack"]
students[1...2] = ["Haha", "Hongchul"]
//["Maxime", "Haha", "Hongchul"]
// ์์ ํ ๋ฐฉ๋ฒ
let oddNumbers = [1, 3, 5, 7, 9, 11, 13, 15]
if let firstElement = oddNumbers.first, let lastElement = oddNumbers.last {
print(firstElement, lastElement, separator: ", ")
}
// print 1, 15
let emptyDouble: [Double] = []
print(emptyDouble.first, emptyDouble.last, separator: ", ")
//๋ง์ฝ array๊ฐ empty๋ผ๋ฉด nill ์ถ๋ ฅ
// print nil, nil
// a
print(emptyDouble[0])
// Triggers runtime error: Index out of range
let cities:[String] = ["์งํด","๋ง์ฐ","์ฐฝ์"]
for city in cities {
print("I don't live in \(city)")
}
// I don't live in ์งํด
// I don't live in ๋ง์ฐ
// I don't live in ์ฐฝ์
for(n,c) in cities.enumerated(){
print("\(n): '\(c)'")
}
// 0: '์งํด'
// 1: '๋ง์ฐ'
// 2: '์ฐฝ์'
var numbers: [Int] = [1,2,3,4,5]
var numbersCopy = numbers
numbers[0] = 100
print(numbers)
// [100, 2, 3, 4, 5]
print(numbersCopy)
// [1, 2, 3, 4, 5]
๋ค์๊ณผ ๊ฐ์ด numbersCopy์ numbers๋ฅผ ๋ณต์ฌํ๊ณ , numbers์ ๊ฐ์ ์์ ํ์ฌ๋ ๋
๋ฆฝ์ ์ธ ์ธ์คํด์ค ์ด๋ฏ๋ก ์๋ก์ ๊ฐ์ด ๋ค๋ฅธ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ฆ Swift์ Value Types ์ธ์คํด์ค๋ ๋ณต์ฌ๋ฅผ ํ ๋ ์ฐธ์กฐํ๋ ๊ฐ์ฒด์ ์ฃผ์๋ฅผ ๊ณต์ ํ๋ ๊ฒ์ด ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋
๋ฆฝ์ ์ผ๋ก ๊ฐ์ง๋ค.
var firstInteger = [IntegerReference(),IntegerReference()]
var secondInteger = firstInteger
firstInteger[0].value = 100
print(secondInteger[0].value)
// Prints "100"
firstInteger[0] = IntegerReference()
print(firstIntegers[0].value)
// Prints "10"
print(secondInteger[0].value)
// Prints "100"
์ด ๊ฒฝ์ฐ๋ ๋ฐฐ์ด์ element๊ฐ class์ ์ธ์คํด์ค์ด๋ค class๋ Swift์์ Reference Types์ด๊ณ , Reference Types์ ์ธ์คํด์ค๋ ๋ชจ๋ ์ ์ธ์คํด์ค๊ฐ ๋์ผํ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ฐ์ง๋ค
๊ทธ๋ฌ๋ฏ๋ก ํ๊ฐ์ ๋ฐฐ์ด์ ์ฐธ์กฐํ๋ ์ธ์คํด์ค์ ๊ฐ์ด ๋ณํ๋ฉด ์ด ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๋ ๋ค๋ฅธ ๋ฐฐ์ด์ ๊ฐ๋ ๋ณํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๊ทธ๋ฌ๋ ๋ง์ฝ, ๋ฐฐ์ด์ด ์๋ก ์์ฑ๋ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๋ค๋ฉด ์ด ๋์ ์๋ก ์ฐธ์กฐํ๋ ์ฃผ์๊ฐ ๋ค๋ฅด๋ฏ๋ก ์๋ก ๋ ๋ฆฝ์ ๊ด๊ณ๊ฐ ๋๋๊ฒ์ ํ์ธํ ์ ์๋ค.
๊ทธ๋ฌ๋ฉด Swift์ ๋ฐฐ์ด์ ๋ณต์ฌ ์ ๋งค๋ฒ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์๋ก ํ ๋นํ ๊น??
๊ฒฐ๋ก ์ ์๋๋ค!!
Swift์์๋ Value Types์ ๋ํด copy-on-wirte optimization ์ ์ ๊ณตํ๋ค.
A ๋ฐฐ์ด, B ๋ฐฐ์ด์ด ์๊ณ ๋ค์๊ณผ ๊ฐ์ด ๋ณต์ฌํ ๊ฒฝ์ฐ
B = A
์ด ์์ ์์๋ B์ A๋ ๋์ผํ ์ ์ฅ์๋ฅผ ๊ณต์ ํ๋ค. ์ฆ ์ฌ๋ฌ ๋ณต์ฌ๋ณธ์ด ์์ ์ ๊ฑฐ์น๊ธฐ ์ ๊น์ง๋ ๋์ผํ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ณต์ ํ๋ ๊ฒ์ด๋ค. ๋ค์๊ณผ ๊ฐ์ด A ๋ฐฐ์ด์์ ์์ ์ด ๋ฐ์ํ๋ค๋ฉด
A[0] = 11
A๋ ํด๋น ๋ฐ์ดํฐ์ ๋ณต์ฌ๋ณธ์ ์๋ก ๋ง๋ค๊ณ ์ด๋ฅผ ์์ ํ๋ ๊ฒ์ด๋ค. ๊ฒฐ๊ตญ ๊ธฐ์กด์๋ ๋์ผํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณต์ ํ๊ณ ํ์ํ ๋๋ง ๋ณต์ฌํ์ฌ ๋ณต์ฌ๋์ ์ค์ด๊ณ ์ต์ ํ๋ฅผ ์ํค๋ ๊ฒ์ด๋ค. ์ด๊ฒ์ copy-on-write ๋ฐฉ์์ด๋ผ๊ณ ํ๋ค.
๋ค์ ์์์์๋ ๋์ผํ storage๋ฅผ ๊ณต์ ํ๋ ๋ ๊ฐ์ ๋ณต์ฌ๋ณธ๊ฐ ์ซ์ ๋ฐฐ์ด์ด ์์ฑ๋๋ค. ์๋์ ๋ฐฐ์ด์ด ์์ ๋๊ธฐ ์ ์ ๋ฐ์ดํฐ์ ํด๋นํ๋ ์ ์ฅ์์ ๋ ๋ฆฝ์ ์ธ ๋ณต์ฌ๋ณธ์ ๋ง๋ค๊ณ , ์ด ๋ณต์ฌ๋ณธ์ ๋ํด ์์ ์ด ์ด๋ฃจ์ด์ง๋ค.
var numbers = [1,2,3,4,5]
var firstCopy = numbers
var secondCopy = numbers
// storage copy
numbers[0] = 100
numbers[1] = 200
numbers[2] = 300
// 'numbers' is [100, 200, 300, 4, 5]
// 'firstCopy' and 'secondCopy' are [1, 2, 3, 4, 5]