ν¨μ: func ν€μλλ₯Ό μ¬μ©νμ¬ μ μ, -> λ₯Ό μ¬μ©ν΄μ ν¨μμ λ°ν νμ μ§μ
func hello(name: String, time: Int) -> String {
var string = ""
for _ in 0.. < time {
string += "\(name)λ μλ
νμΈμ!\n"
}
return string
}
ν¨μ νΈμΆ μ, νλΌλ―Έν° μ΄λ¦μ ν¨κ» μ¨μ£Όμ΄μΌ ν¨
hello(name: "nang", time: 4)
ν¨μ νΈμΆ μ, νλΌλ―Έν° μ΄λ¦κ³Ό ν¨μ λ΄λΆμμ μ¬μ©νλ νλΌλ―Έν° μ΄λ¦μ λ€λ₯΄κ² μ¬μ©ν κ²½μ°
func hello(to name: String, numberOfTimes time: Int) {
//ν¨μ λ΄λΆμμλ nameκ³Ό timeμ μ¬μ©
for _ in 0.. < time {
print(name)
}
}
hello(to: "nang", numberOfTimes:4) //toμ numberOfTimes μ¬μ©
νλΌλ―Έν° μ΄λ¦μ _λ‘ μ μνλ©΄ ν¨μ νΈμΆ μ, νλΌλ―Έν° μ΄λ¦ μλ΅ κ°λ₯
func hello(_ name: String, time: Int) {
// ...
}
hello("nang", time:4)
νλΌλ―Έν°μ κΈ°λ³Έκ° μ§μ κ°λ₯, κΈ°λ³Έ κ°μ΄ μ§μ λ νλΌλ―Έν°λ ν¨μ νΈμΆμ μλ΅ κ°λ₯
func hello(name: String, time: Int = 1) {
// ...
}
hello("nang")
...μ μ¬μ©νλ©΄ κ°μκ° μ ν΄μ§μ§ μμ νλΌλ―Έν°λ₯Ό λ°μ μ μμ
func sum(_numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sum(1,2)
sum(3,4,5)
ν¨μ μμ ν¨μ μμ± κ°λ₯
func hello(name: String. time: Int) {
func message(name: String) -> String {
return "\(name)λ μλ
νμΈμ!"
}
for _ in 0.. < time {
print(message(name: name))
}
}
ν¨μ μμ μ μν ν¨μ λ°ν κ°λ₯
helloGenerator() ν¨μμ λ°ν νμ
μ (String) -> String
-> λ¬Έμμ΄μ λ°μμ λ¬Έμμ΄μ λ°ννλ ν¨μ
func helloGenerator(message: String) -> (String) -> String {
func hello(name: String) -> String {
return name + message
}
return hello
}
let hello = helloGenerator(message: "λ μλ
νμΈμ!")
hello("λ")
ν¨μ μμ μ μν ν¨μκ° μ¬λ¬ κ°μ νλΌλ―Έν°λ₯Ό λ°λ κ²½μ°
(String, String) -> String
: λ¬Έμμ΄ λ κ°λ₯Ό λ°μμ λ¬Έμμ΄μ λ°ννλ μλ―Έ
func helloGenerator(message: String) -> (String,String) -> String {
func hello(firstName: String, lastName: String) -> String {
return lastName + firstName + message
}
return hello
}
let hello = helloGenerator(message: "λ μλ
νμΈμ!")
hello("λμ","μ ")
ν΄λ‘μ : μ€κ΄νΈ( {} )λ‘ κ°μΈμ§ 'μ€ν κ°λ₯ν μ½λ λΈλ'
func helloGenerator(message: String) -> (String, String) -> String {
return { (firstName: String, lastName: String) -> String in
return lastName + firstName + message
}
}
ν¨μμλ λ€λ₯΄κ² ν¨μ μ΄λ¦ μ μκ° λ°λ‘ μ‘΄μ¬νμ§ μμ
but, νλΌλ―Έν°λ₯Ό λ°μ μ μκ³ , λ°ν κ°μ΄ μ‘΄μ¬
=> ν¨μλ μ΄λ¦μ΄ μλ ν΄λ‘μ
func helloGenerator(message: String) -> (String, String) -> String {
return { firstName, lastName in
return lastName + firstName + message
}
}
π°
func helloGenerator(message: String) -> (String, String) -> String {
return {
return $1 + $0 + message
}
}
ν΄λ‘μ λ΄λΆμ μ½λκ° ν μ€μ΄λΌλ©΄, return μλ΅ κ°λ₯
func helloGenerator(message: String) -> (String, String) -> String {
return { $1 + $0 + message }
}
ν΄λ‘μ λ λ³μμ²λΌ μ μ κ°λ₯
let hello: (String, String) -> String = { $1 + %0 + "λ μλ
νμΈμ!" }
hello("λμ","μ ")
μ΅μ λλ‘λ μ μ κ°λ₯
let hello: ((String, String) -> String)?
hello?("λμ", "μ ")
ν΄λ‘μ λ₯Ό λ³μλ‘ μ μνκ³ ν¨μμμ λ°νν μλ μλ κ²μ²λΌ, νλΌλ―Έν°λ λ°μ μ μμ
func manipulate(number: Int, using block: Int -> Int) -> Int {
return block(number)
}
manipulate(number: 10, using: { (number: Int) -> Int in
return number * 2
})
π°
manipulate(number: 10, using: {
$0 * 2
})
(λ¨μ½ ν¨μμ λ§μ§λ§ νλΌλ―Έν°κ° ν΄λ‘μ λΌλ©΄, κ΄νΈμ νλΌλ―Έν° μ΄λ¦ μλ΅ κ°λ₯)
manipulate(number: 10) {
$0 * 2
}
π sort() μ filter()
ν¨μκ° ν΄λ‘μ νλλ§μ νλΌλ―Έν°λ‘ λ°λλ€λ©΄, κ΄νΈλ₯Ό μμ μ°μ§ μμλ λ¨
let numbers = [1,3,2,6,7,5,8,4]
let sortedNumbers = numbers.sort { $0 < $1 }
print(sortedNumbers) //[1,2,3,4,5,6,7,8]
let evens = numbers.filter { $0 % 2 == 0 }
print(evens) //[2,6,8,4]
ν΄λμ€λ class λ‘, ꡬ쑰체λ struct λ‘ μ μ
class Dog {
var name: String?
var age: Int?
func simpleDescription() -> String {
if let name = self.name {
return "πΆ \(name)"
} else {
return "πΆ No name"
}
}
}
struct Coffee {
var name: String?
var size: String?
func simpleDescription() -> String {
if let name = self.name {
return "βοΈ \(name)"
} else {
return "βοΈ No name"
}
}
}
var myDog = Dog()
myDog.name = "λ£½μ§"
myDog.age = 3
print(myDog.simpleDescription())
var myCoffee = Coffee()
myCoffee.name = "μλ©λ¦¬μΉ΄λ
Έ"
myCoffee.size = "Tall"
print(myCoffee.simpleDescription())
ν΄λμ€λ μμ κ°λ₯, ꡬ쑰체λ λΆκ°λ₯
class Animal {
let numberOfLegs = 4
}
class Dog: Animal {
var name: String?
var age: Int?
}
var myDog = Dog()
print(myDog.numberOfLegs) //Animal ν΄λμ€λ‘λΆν° μμλ°μ κ° (4)
ν΄λμ€λ μ°Έμ‘° (Reference),
ꡬ쑰체λ λ³΅μ¬ (Copy)
var dog1 = Dog() //dog1μ μλ‘ λ§λ€μ΄μ§ Dog()λ₯Ό μ°Έμ‘°
var dog2 = dog1 //dog2λ dog1μ μ°Έμ‘°νλ Dog()λ₯Ό λκ°μ΄ μ°Έμ‘°
dog1.name = "λ£½μ§" /dog1μ μ΄λ¦μ λ°κΎΈλ©΄ Dog()μ μ΄λ¦μ΄ λ°λ
print(dog2.name) //dog2μ μ΄λ¦μ κ°μ Έμλ λ°λ μ΄λ¦ μΆλ ₯
var coffee1 = Coffee() //coffee1μ μλ‘ λ§λ€μ΄μ§ Coffee() κ·Έ μ체
var coffee2 = coffee1 //coffee2λ coffee1μ 볡μ¬ν κ°
coffee1.name = "μλ©λ¦¬μΉ΄λ
Έ"
coffee2.name //coffee1μ μ΄λ¦μ λ°κΏλ coffee2λ λ³κ°μ΄κΈ° λλ¬Έμ λ°λμ§ μμ (nil)
ν΄λμ€μ ꡬ쑰체 λͺ¨λ μμ±μλ₯Ό κ°μ§κ³ μμ.
μμ±μμμλ μμ±μ μ΄κΈ°κ° μ§μ κ°λ₯
class Dog {
var name: String?
var age: Int?
init() {
self.age = 0
}
}
struct Coffee {
var name: String?
var size: String?
init() {
self.size = "Tall"
}
}
μμ±μ΄ μ΅μ
λμ΄ μλλΌλ©΄ νμ μ΄κΈ°κ°μ κ°μ ΈμΌ νλ€.
μ΅μ
λμ΄ μλ μμ±μ΄ μ΄κΈ°κ°μ κ°μ§κ³ μμ§ μμΌλ©΄ μ»΄νμΌ μλ¬ λ°μ
class Dog {
var name: String?
var age: Int //μ»΄νμΌ μλ¬
}
class Dog {
var name: String?
var age: Int = 0 //μμ± μ μν λ μ΄κΈ°κ° μ§μ
}
μμ±μ μ μν λ,
1) μ΄κΈ°κ°μ μ§μ ν΄ μ£Όλ λ°©λ²
class Dog {
var name: String?
var age: Int = 0
}
2) μμ±μμμ μ΄κΈ°κ°μ μ§μ ν΄ μ£Όλ λ°©λ²
class Dog {
var name: String?
var age: Int
init() {
self.age = 0
}
}
ν¨μμ λ§μ°¬κ°μ§λ‘ νλΌλ―Έν°λ₯Ό λ°μ μ μμ
class Dog {
var name: String?
var age: Int
init(name: String?, age: Int) {
self.name = name
self.age = age
}
}
var myDog = Dog(name: "λ£½μ§", age: 3)
λ§μ½ μμλ°μ ν΄λμ€λΌλ©΄ μμ±μμμ μμ ν΄λμ€μ μμ±μλ₯Ό νΈμΆν΄μ£Όμ΄μΌ ν¨
λ§μ½ μμ±μμ νλΌλ―Έν°κ° μμ ν΄λμ€μ νλΌλ―Έν°μ κ°λ€λ©΄ override ν€μλ μ¬μ©
super.init() μ ν΄λμ€ μμ±λ€μ μ΄κΈ°κ°μ΄ λͺ¨λ μ€μ λ νμ ν΄μΌνλ€.
νμ μκΈ° μμ μ λν self ν€μλ μ¬μ© κ°λ₯
class Dog: Animal {
var name: String?
var age: Int
override init() {
self.age = 0 // μ΄κΈ°κ° μ€μ
super.init() //μμ ν΄λμ€ μμ±μ νΈμΆ
print(self.simpleDescription()) //μ¬κΈ°μλΆν° self μ κ·Ό κ°λ₯
}
func simpleDescription() -> String {
if let name = self.name {
return "πΆ \(name)"
} else {
return "πΆ No name"
}
}
}
-> super.init() μ νκΈ° μ μ self μ μ κ·Όνλ€λ©΄ μ»΄νμΌ μλ¬!
struct Hex {
var decimal: Int?
var hexString: String? {
get {
if let decimal = self.decimal {
return String(decimal, radix: 16)
} else {
return nil
}
}
set {
if let newValue = newValue {
self.decimal = Int(newValue, radix: 16)
} else {
self.decimal = nil
}
}
}
}
var hex = Hex()
hex.decimal = 10
hex.hexString //"a"
hex.hexString = "b"
hex.decimal //11
decimal: Stored Property
hexString: Computed Property
get,set κ³Ό λΉμ·ν willSet, didSet μ μ΄μ©νλ©΄
μμ±μ κ°μ΄ μ§μ λκΈ° μ§μ κ³Ό μ§νμ μνλ μ½λ μ€ν κ°λ₯
struct Hex {
var decimal: Int? {
willSet {
print("\(self.decimal)μμ \(newValue)λ‘ κ°μ΄ λ°λ μμ μ
λλ€.")
}
didSet {
print("\(oldValue)μμ \(self.decimal)λ‘ κ°μ΄ λ°λμμ΅λλ€.")
}
}
}
willSet : μλ‘μ΄ κ°μ newValueλ‘ μ»μ΄μ΄
didSet : μμ κ°μ oldValue λΌλ μμ½μ΄λ₯Ό ν΅ν΄ μ»μ΄μ΄
: κ°λ€μ λ¬Άμ
κ°μ μ κ·Όν λμλ . μ¬μ©
var coffeeInfo = ("μλ©λ¦¬μΉ΄λ
Έ", 5100)
coffeeInfo.0 //μλ©λ¦¬μΉ΄λ
Έ
coffeeInfo.1 //5100
νλΌλ―Έν°μ μ΄λ¦μ λΆμΌ μ μμ
var namedCoffeeInfo = (coffee: "μλ©λ¦¬μΉ΄λ
Έ", price: 5100)
namedCoffeeInfo.coffee //μλ©λ¦¬μΉ΄λ
Έ
namedCoffeeInfo.price //5100
namedCoffeeInfo.price = 5100
var coffeeInfo: (String, Int)
var namedCoffeeInfo: (coffee: String, price: Int)
μ¬λ¬ λ³μμ κ° μ§μ
let (coffee, price) = ("μλ©λ¦¬μΉ΄λ
Έ", 5100)
coffee //μλ©λ¦¬μΉ΄λ
Έ
price //5100
ννμ΄ κ°μ§ κ°μΌλ‘ λ³μμ κ°μ μ§μ ν λ, 무μν κ°μ _ ν€μλλ₯Ό μ¬μ©
let (_, latteSize, lattePrice) = ("λΌλΌ", "Venti", 5600)
latteSize //Venti
lattePrice //5600
ννμ λ°ννλ ν¨μ
//μ»€νΌ μ΄λ¦μ λ§λ μ»€νΌ κ°κ²© μ 보 λ°ν. μΌμΉνλ μ΄λ¦μ΄ μλ€λ©΄ 'nil' λ°ν
func coffeeInfo(for name: String) -> (name: String, price: Int)? {
let coffeeInfoList: [(name: String, price: Int)] = [
("μλ©λ¦¬μΉ΄λ
Έ", 5100),
("λΌλΌ", 5600),
]
for coffeeInfo in CoffeeInfoList {
if coffeeInfo.name == name {
return coffeeInfo
}
}
return nil
}
coffeeInfo(for: "μλ©λ¦¬μΉ΄λ
Έ")?.price //5100
coffeeInfo(for: "μμ€νλ μ")?.price //nil
let (_, lattePrice) = coffeeInfo(for: "λΌλΌ")!
lattePrice //5600