μ€λμ swiftμ κΈ°λ³Έ λ¬Έλ²λΆν° ν¨μκΉμ§ ! πΈ
π κΈ°λ³Έ λ¬Έλ² π
λ³μ μμ
var r = 3.123// λ³νλ κ°
let p = 3.1415926535 // λ³νμ§ μλ κ°
var message : String = "νμ΄"
// Bool, Int, String, Float, Double ...
cmd + click → λ³μλͺ νλ²μ λ³κ²½ κ°λ₯
λ°μ΄ν°μ μλ£νμ λ³΄κ³ νμ μΆλ‘ → λ°μ΄ν°κ° μμΌλ©΄ μλ£ν λͺ μ νμ
νλ² μλ£νμ΄ μ ν΄μ§λ©΄ λ³κ²½ λΆκ°λ₯
Intνμ νλ«νΌμ λ°λΌ λ²μκ° λ¬λΌμ§
Int8, 16, 32, 64λ‘ λ€μνλ° μ¬μ©κ°λ₯ λ²μκ° λμ΄κ°κ² λλ©΄ μ€λ²νλ‘μ° λ°μ
Intλ‘ μ μΈνκ² λλ©΄ μμ μ cpuμ λ§κ² μλμ μΌλ‘ μ€μ λ¨
ν¨μ
// ν¨μ μ μΈ
func circleArea(r: Double) -> Double {
return (r * r * 3.1415926535)
}
// ν¨μ μ¬μ©
circleArea(r: 3.123)
class
class Circle {
var radius = 3.123
let pi = 3.1415926535
func circleArea(r: Double) -> Double {
return (r * r * 3.1415926535)
}
}
κ΄λ ¨μ΄ μλ κ²λΌλ¦¬ λ¬Άκ³ νμν λλ§λ€ μ¬μ©
κΈ°μ‘΄μ class μ¬μ© or μ§μ class λ§λ€μ΄ μ¬μ©
μ°μ°μ
- μ°μ μ°μ°μ ( +, - , *, /, %)
- λΉκ΅ μ°μ°μ ( <, >, <=, >=, ==, != )
- λ Όλ¦¬ μ°μ°μ ( &&, ||, ! )
쑰건문
- if문
var num = 10;
if (num < 5)
{
print("5λ³΄λ€ μμ΅λλ€ !")
}
else if (num == 5)
{
print("5μ
λλ€ !")
}
else
{
print("5λ³΄λ€ ν½λλ€ !")
}
- switch문
var num = "μ₯λ§";
switch num
{
case "λ§μ":
print("λ§μ")
case "νλ¦Ό":
print("νλ¦Ό")
case "λΉ", "μ₯λ§":
print("λΉλ μ₯λ§")
default:
print("λͺ°λΌμ© ~")
}
cμΈμ΄μ λ€λ₯Έ μ 1. breakλ¬Έ νμ μμ 2. ν caseλΉ μ¬λ¬ 쑰건 κ°λ₯ 3. default μμΌλ©΄ μ€λ₯ λ±μ
λ°λ³΅λ¬Έ
- for문
for i in 1...10
{
print(i)
}
for i in stride(from: 1, to: 10, by: 2)
{
print("κ°μ: \\(i)")
}
for i in stride(from: 10, to: 1, by: -2)
{
print("κ°μ: " + String(i))
}
- while문 + repeat while문
var i = 0
while (i < 10)
{
i += 1
print(i)
}
i = 0
repeat
{
i += 1
print(i)
} while (i < 10)
λ°°μ΄
var array = [1, 2, 3, 4]
var evenArray: Array<Int> = [2, 4, 6, 8]
var charArray: [String] = ["one", "two", "three"]
array[0] // κ° κ°μ Έμ€κΈ° -> 1
array.append(5) // λ°°μ΄ λ§¨ λμ 5 μΆκ°
array.insert(-1, at: 0) // 0λ²μ§Έ μΈλ±μ€μ -1 μΆκ°
array.remove(at: 0) // 0λ²μ§Έ μΈλ±μ€ μμ
for i in array
{
print(i)
}
λ€μν ν¨μλ₯Ό ν΅ν΄ λ°°μ΄μ μΆκ°, μμ κ° μ©μ΄
λμ λ리
var dic = [1: "one", 2: "two", 3: "three"] // ν€: λ°Έλ₯
var dic1: Dictionary<Int, String> = [1: "one", 2: "two", 3: "three"]
dic[1] // κ° κ°μ Έμ€κΈ° -> one
dic[4] = "four" // κ° μΆκ°
dic.removeValue(forKey: 4) // κ° μμ
dic[3] = nil // κ° μμ
dic1.removeAll() // μ 체 μμ , μ΄κΈ°ν
for (key, value) in dic
{
print("key: \\(key), value: \\(value)")
} // λ°λ³΅λ¬Έμμ λμ
λ리 μ¬μ©
for key in dic.keys
{
print(key)
} // λ°λ³΅λ¬Έμμ λμ
λ리μ ν€λ§
for value in dic.values
{
print(value)
}
π ν¨μ π
κΈ°λ³Έ νν
func hello(name: String) -> String {
return ("Hello~ " + name)
}
print(hello(name: "mark"))
λ°νκ°μ΄ voidμΈ κ²½μ° μλ΅κ°λ₯
λ€μν νν
// κΈ°λ³Έ νν
func addTwoNum(num1: Int, num2: Int) -> Int {
return (num1 + num2)
}
addTwoNum(num1: 1, num2: 2)
// λ§€κ° λ³μμ κΈ°λ³Έ κ°μ΄ μλ κ²½μ°
func add100(num1: Int, num2: Int = 100) -> Int {
return (num1 + num2)
}
add100(num1: 100)
add100(num1: 100, num2: 200) // μ΄λ° μμΌλ‘ λ³κ²½ κ°λ₯
// κ°λ³ μΈμλ₯Ό μ¬μ©ν΄μ λ§€κ° λ³μκ° μ¬λ¬ κ°μΈ κ²½μ°
func addNums(nums: Int...) -> Int {
var sum = 0
for num in nums
{
sum += num
}
return (sum)
}
addNums(nums: 1, 2, 3, 4, 5)
// λ°νκ°μ΄ μ¬λ¬ κ°μΈ κ²½μ°
func myInfo() -> (name: String, age: Int) {
return ("mark lee", 24)
}
myInfo().name
myInfo().age
print(myInfo())
Argument label
func calc(firstNum num1: Int, secondNum num2: Int) -> Int {
return (num1 + num2)
}
calc(firstNum: 1, secondNum: 2)
// μΈλλ°λ‘ μλ΅κ°λ₯
func calc2(_ num1: Int, _ num2: Int) -> Int {
return (num1 + num2)
}
calc2(1, 2) // λ§€κ° λ³μλͺ
μμ΄ μ¬μ© κ°λ₯
λ§€κ° λ³μμ λν μ€λͺ μ ν΄μ€
μΈλλ°λ‘ μλ΅ κ°λ₯ → ν¨μ νΈμΆν λλ μ λ ₯κ°λ§ λ£μ μ μμ
inout
func addTwoNum(num1: Int, num2: inout Int) -> Int {
num2 = 0
return (num1 + num2)
}
var num1 = 1
var num2 = 2
addTwoNum(num1: num1, num2: &num2)
inout ν€μλ μμ΄ λ§€κ° λ³μμ κ°μ λ°κΏ μ μμ ! → μλ¬
inout ν€μλκ° μλ κ²½μ° ν¨μ νΈμΆ μ μ£Όμ μ°μ°μ λΆμ¬μΌ ν¨
- 16μ§μ μ¬μ©μ΄μ : λ°μ΄νΈμ μ λ§μ + κ°κ²°νκ³ μ§§μ + 2μ§μμ μνΈλ³νμ΄ μ¬μ(4μλ¦¬μ© λμ΄μ)
- μ©μ΄ μ 리
func calc(firstNum num1: Int, secondNum num2: Int) -> Int {
return (num1 + num2)
}
// func : ν¨μ ν€μλ
// calc : ν¨μ μ΄λ¦
// num1 : νλΌλ―Έν° λ€μ == 맀κ°λ³μ μ΄λ¦
// Int : νλΌλ―Έν° νμ
, λ¦¬ν΄ νμ
// firstNum : argument label == μΈμμ λν μ€λͺ
calc(firstNum: 10, secondNum: 10) // argument == μΈμ
λ !!
κ·Έλλ cλ κ°λ μ κ±°μ λΉμ·ν΄μ μ¬μ λ€! νμλ§ μμ μ΅μΌλ©΄ λ λ―
'c o d i n g . . π > ios' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μΈνλ°] μμ΄ν° μ± κ°λ°(4) (0) | 2022.06.22 |
---|---|
[μΈνλ°] μμ΄ν° μ± κ°λ°(3) (0) | 2022.06.21 |
[μΈνλ°] μμ΄ν° μ± κ°λ°(1) (0) | 2022.06.17 |