[인프런 강의] Swift - 3 강의 정리

2019. 7. 21. 17:27

Function, 함수 실습

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
func sayHello1() {
    print("Hello This is TestPage")
}
 
sayHello1()
 
func sayHello2(insertYourname name: String, internationalAge age: Int){
    print("Hello \(name), Your \(age) years old!")
}
 
sayHello2(insertYourname: "Kim", internationalAge: 34)
 
 
func sayHello3(name: String = "fixname"){
    print("Hello \(name)! isEmpty?")
}
 
sayHello3(name: "Have")
sayHello3() //no parameter
 
func sayHello4(_ name: String, _ age: Int-> String {
    return "\(name) is \(age) years old"
}
 
var info = sayHello4("choi"20)
print(info)
 
 

//파라미터 없는 함수를 간단히 사용하는 법

//argument label을 파라미터 name과 다르게 설정해서 사용하는 방법)

//입력값이 없을 시에 기본으로 설정되는 디폴트 값 지정 함수

//return 타입이 String이고, argument label을 따로 지정해주지 않는 법

 

Eumerations 

 

이 타입을 써야하는 것이 몇 개가 있다. 간단히 알고 넘어가자.

관련된 값들의 그룹을 위해 사용하는 것, 코드의 안정화를 위해 많이 쓰는 값들을 모아놓은 것

(약간 C언어에서 구조체같은 느낌)

1
2
3
4
5
6
7
8
9
10
enum CompassPoint {
    case north
    case south
    case east
    case west
 
}
 
//사용시에
var direction = CompassPiont.west //하나씩만 사용하게
direction = .east 도 가능
 

 

Class and Structure

 

목적은 비슷하지만 , 클래스가 사용하는 방법이 더 많다. 유연성있는 구조를 만들기 위해서 사용한다.

클래스 안에서 쓰이는 constant, varialbe은 properties라고 명칭이 바뀌고,

클래스 안에서 쓰이는 function은 method라고 불린다.

스위프트에서 클래스를 정의할 때는 한 파일씩 나눠서 작성하고 사용한다.

 

클래스가 구조체와 다른 점>

클래스는 상속을 할 수 있다.

형변환을 해서 쓸 수 있다.

메모리에 올라가서 객체들이 사용되는 것을 카운팅을 할 때, 이런것들을 더 쓸수 있다. 등등

 

- Properties: var firstBValeue(저장 properites)/ get-set(Computed properites)

- Method == function

- Inheritance: 다른 클래스에서 구현해 놓은 것을 다시 받아다가 쓰겠다(상속 개념), 중복 코드x, 줄어드는 코드

- Overriding: 재정의를 한다는 뜻, 이걸 해야지 로드해 올 수 있음

- Initialization: 초기화(초기값 세팅하는 것), init()

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import UIKit
 
class vehicle{
    var currentSpeed = 0.0 ///stored property
    var description:String {
        return "Traveling at \(currentSpeed) miles per hour"
    }
    func makeNoise() { //함수 선언(메서드 선언)
        print("noisesless")
    }
    
}
 
let someVehicle = vehicle()
someVehicle.currentSpeed = 1.0
print(someVehicle.description)
print(someVehicle.currentSpeed)
someVehicle.makeNoise()
 
//상속하기
class Bicycle: vehicle {
    var hasBesket = false
}
 
let someBicycle = Bicycle() //create class
 
someBicycle.currentSpeed = 15.0
someBicycle.hasBesket = true
print(someBicycle.currentSpeed)
print(someBicycle.hasBesket)
 
 
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Tandem:Bicycle {
    var currentNumberPaseengers = 0
    override var description: String{
        return "Traveling at \(currentSpeed) miles perhour, number of passenger : \(currentNumberPaseengers)"
    }
}
let someTandem = Tandem()
someTandem.hasBesket = true
someTandem.currentNumberPaseengers = 2
someTandem.currentSpeed = 22.0
 
print("Tandem : \(someTandem.description)")
 
class Train:vehicle {
    override func makeNoise() {
        print("choo choo!")
    }
}
 
let someTrain = Train()
someTrain.makeNoise()
 
class car:vehicle {
    var gear = 1
    override init(){
        print("car")
    }
    init(newGear: Int){
        gear = newGear
    }
}
 
let someCar = car() //initialize
 
let someCar2 = car.init(newGear: 5)
 
 

 

 

BELATED ARTICLES

more