π λ³μμ μμ
λ³μ(variable)λ κ°μ μμ ν μ μκ³ , μμ(constant)λ μμ λΆκ°
var name = "nang"
let birthyear = 1999
Swiftλ μ μ νμ΄ν μΈμ΄
λ³μλ μμλ₯Ό μ μν λ μλ£ν(νμ
)μ λͺ
μν΄μ£Όμ΄μΌ ν¨
var name: String = "nang"
let birthyear: Int = 1999
var height: Float = 163.0
birthyear + height //μ»΄νμΌ μλ¬
Float(birthyear) + height
String(birthyear) + "λ
μ νμ΄λ " +name+ "!"
π°
"\(birthyear) λ
μ νμ΄λ \(name)!"
π νμ
μ΄λ
Έν
μ΄μ
(Type Annotation)
: λ³μλ μμ μ΄λ¦ λ€μ μ½λ‘ (:)μ λΆμ΄κ³ μλ£νμ μ¨μ£Όλ κ²
π νμ
μΆλ‘ (Type Inference)
: νμ
μ μ§μ λͺ
μνμ§ μκ³ λ κ°μ κ°μ§κ³ μ μ νμ΄νμ ν μ μκ² νλ κ²
π λ°°μ΄κ³Ό λμ λ리
λ°°μ΄κ³Ό λμ λ리 λͺ¨λ λκ΄νΈ([])λ₯Ό μ¬μ©ν΄μ μ μ
var languages = ["Swift", "Objective-C", "Python"]
var capitals = [
"νκ΅" : "μμΈ",
"μΌλ³Έ" : "λμΏ",
"μ€κ΅" : "λ² μ΄μ§",
]
λ°°μ΄κ³Ό λμ λ리μ μ κ·Όνκ±°λ κ°μ λ³κ²½ν λμλ λκ΄νΈ μ¬μ©
languages[0]
languages[1] = "Ruby"
capitals["νκ΅"]
capirals["νλμ€"] = "ν리"
π‘ μ°Έκ³
λ°°μ΄κ³Ό λμ
λ리λ₯Ό letμΌλ‘ μ μνλ©΄ κ° μμ λΆκ°
languages μ capitalsμ νμ
var languages: [String] = ["Swift", "Objective-C", "Python"]
var capitals: [String: String] = [
"νκ΅" : "μμΈ",
"μΌλ³Έ" : "λμΏ",
"μ€κ΅" : "λ² μ΄μ§",
]
λΉ λ°°μ΄μ΄λ λΉ λμ λ리 μ μ
var languages: [String] = []
var capitals: [String: String] = [:]
or
var languages = [String]()
var capitals = [String: String]()
π 쑰건문과 λ°λ³΅λ¬Έ
var age = 24
var student = ""
if age >= 8 && age < 14 {
student = "μ΄λ±νμ"
} else if age < 17 {
student = "μ€νμ"
} else if age < 20 {
student = "κ³ λ±νμ"
} else {
student = "κΈ°ν"
}
student //κΈ°ν
if λ¬Έμ 쑰건μ μλ κ°μ΄ μ ννκ² μ°Έ λλ κ±°μ§μΌλ‘ λμ€λ Bool νμ μ¬μ©
var number = 0
if !number { //μ»΄νμΌ μλ¬
}
if number == 0 {
//
}
λΉ λ¬Έμμ΄μ΄λ λ°°μ΄μ κ²μ¬ν λμλ κΈΈμ΄κ° 0μΈμ§ κ²μ¬
if name.isEmpty {...}
if languages.isEmpty {...}
switchλ¬Έ: Swiftμ switch ꡬ문μ ν¨ν΄ 맀μΉμ΄ κ°λ₯
switch age {
case 8.. < 14:
student = "μ΄λ±νμ"
case 14.. < 17:
student = "μ€νμ"
case 17.. < 20:
student = "κ³ λ±νμ"
default:
student = "κΈ°ν"
}
for,while : λ°λ³΅λλ μ°μ°μ ν λ μ¬μ©
for language in languages {
print("μ λ \(language) μΈμ΄λ₯Ό λ€λ£° μ μμ΅λλ€.")
}
for (country, capital) in capitals {
print("\(country)μ μλλ \(capital)μ
λλ€.")
}
λ°λ³΅λ¬Έ λ§λ€κΈ°
for i in 0..<100 {
i
}
for _ in 0..<10 {
print("Hello")
}
var i = 0
while i < 100 {
i += 1
}
π μ΅μ
λ (Optional)
: κ°μ΄ μμ μλ μκ³ μμ μλ μλ κ²
nil : κ°μ΄ μλ κ²½μ° μ¬μ©
var name: String = "nang"
name = nil //μ»΄νμΌ μλ¬
κ°μ΄ μμ μλ μκ³ μμ μλ μλ λ³μλ₯Ό μ μν λμλ νμ
μ΄λ
Έν
μ΄μ
μ ?λ₯Ό λΆμ
μ΄λ κ² μ μν λ³μκ° μ΅μ
λ
μ΅μ
λμ μ΄κΈ°κ°μ μ§μ νμ§ μμΌλ©΄ κΈ°λ³Έκ°μ nil
var email: String?
print(email) //nil
email = "https://github.com/nang518"
print(email) //Optional("https://github.com/nang518")
μ΅μ λλ‘ μ μν λ³μλ μ΅μ λμ΄ μλ λ³μμ λ€λ¦
let optionalEmail: String? = "https://github.com/nang518"
let requiredEmail: String = optionalEmail //μ»΄νμΌ μλ¬
requiredEmail λ³μλ Stringμ΄κΈ° λλ¬Έμ νμ κ°μ κ°μ§κ³ μμ΄μΌ νλ λ°λ©΄,
optionalEmail μ μ΅μ
λλ‘ μ μΈλ λ³μμ΄κΈ° λλ¬Έμ μ½λκ° μ€νλκΈ° μ κΉμ§λ κ°μ μ 무λ₯Ό μ μ μμ
π μ΅μ
λ λ°μΈλ© (Optional Binding)
: μ΅μ
λμ κ°μ κ°μ Έμ€κ³ μΆμ κ²½μ° μ¬μ©
if let email = optionalEmail {
print(email) //optionalEmailμ κ°μ΄ μ‘΄μ¬νλ κ²½μ° ν΄λΉ κ° μΆλ ₯
} //κ°μ΄ μ‘΄μ¬νμ§ μλλ€λ©΄ if λ¬Έ νμΆ
νλμ if λ¬Έμμ μ½€λ§(,)λ‘ κ΅¬λΆνμ¬ μ¬λ¬ μ΅μ λ λ°μΈλ© κ°λ₯
var optionalName: String? = "nang"
var optionalEmail: String? = "https://github.com/nang518"
if let name = optionalName, email = optionalEmail {
//nameκ³Ό email κ° μ‘΄μ¬
}
π‘μ°Έκ³
μ½λκ° λ무 κΈΈ κ²½μ°μλ, μ¬λ¬ μ€μ κ±Έμ³μ μ¬μ©
λλ²μ§Έ let λΆν°λ μλ΅ κ°λ₯
if let name = optionalName,
let email = optionalEmail {
//nameκ³Ό email κ° μ‘΄μ¬
}
μ΅μ λ λ°μΈλ© ν λ ,λ₯Ό μ¬μ©νμ¬ μ‘°κ±΄ ν¨κ» μ§μ κ°λ₯
var optionalAge: Int? = 24
if let age = optionalAge, age>=20 {
//ageμ κ°μ΄ μ‘΄μ¬νκ³ , 20 μ΄μ
}
π°
if let age = optionalAge {
if age >= 20 {
//ageμ κ°μ΄ μ‘΄μ¬νκ³ , 20 μ΄μ
}
}
π μ΅μ
λ 체μ΄λ (Optional Chaining)
: μ½λλ₯Ό κ°κ²°νκ² λ§λ€μ΄μ£Όλ κ²
let array: [String]? = []
var isEmptyArray = false
if let array = array, array.isEmpty {
isEmptyArray = true
} else {
isEmptyArray = false
}
isEmptyArray
π°
let isEmptyArray = array?.isEmpty == true
-> μ΅μ
λ 체μ΄λμ μ΅μ
λ μμ±μ μ κ·Όν λ,
μ΅μ
λ λ°μΈλ© κ³Όμ μ ? ν€μλλ‘ μ€μ¬μ£Όλ μν
π μ΅μ
λ λ²κΈ°κΈ°
: κ°μ΄ μ‘΄μ¬ν κ²μμλ λΆκ΅¬νκ³ μ΅μ
λλ‘ μ¬μ©ν΄μΌ νλ κ²½μ°, κ°μ΄ μλ€κ³ κ°μ νκ³ κ°μ λ°λ‘ μ κ·Όν μ μλλ‘ λμμ£Όλ ν€μλ (!) μ¬μ©
print(optionalEmail) //Optional("https://github.com/nang518")
print(optionalEmail!) //https://github.com/nang518
βοΈμ£Όμ
μ΅μ
λμ κ°μ΄ nilμΈ κ²½μ°μλ λ°νμ μλ¬ λ°μ
π μ묡μ μΌλ‘ λ²κ²¨μ§ μ΅μ
λ (Implicitly Unwrapped Optional)
: μ΅μ
λμ μ μν λ ? λμ ! λΆμ
var email: String! = "https://github.com/nang518"
print(email)
μ κ·Όν λ μ΅μ
λ λ°μΈλ©μ΄λ μ΅μ
λμ λ²κΈ°λ κ³Όμ μμ΄ λ°λ‘ κ°μ μ κ·Ό κ°λ₯
κ°μ΄ μλλ° μ κ·Όμ μλνλ©΄ λ°νμ μλ¬ λ°μ