Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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
Archives
Today
Total
관리 메뉴

브래의 슬기로운 코딩 생활

iOS프로그래밍 실무 10주차 정리 본문

2-1/iOS프로그래밍 실무

iOS프로그래밍 실무 10주차 정리

김브래 2023. 5. 10. 18:21

당분간 학기 끝날 때 까지 이론은 별로 안할 예정이다.

이제 수업시간에는 거의 실습만 하기 때문에 과제나 실습 화면 캡쳐 본 등을 포스팅 하겠다.

 

- 과제 -

 

기말고사 70%

 

네트워킹(URLSession)

App Transport Security(ATS)

1급 객체(first class object), 1급 시민(first class citizen)

클로저(Closure)

후행 클로저(trailing closure)

디폴트 매개변수(아규먼트)

failable initializer

JSON파싱을 쉽게 하기 위한 구조체 만들기(quicktype)

static metatype

throwing function과 예외처리(exception handling) : do-try-catch

closure 내부에서 self. 사용하는 경우

reloadData()main thread

비동기 처리 : main 스레드, 백그라운드 스레드

옵셔널 바인딩에 guard-let 활용(if-let보다 좋음)

Resolve Auto Layout Issues

앱을 실행하면 어제 날짜로 자동 조회하기

 

네트워킹

 

1. URL 만들기

URL(string: String)

 

2. URLSession 만들기

URLSession(configuration: URLSessionConfiguration)

 

3. URLSession 인스턴스에게 task주기

URLSession인스턴스.dataTask(with:completionHandler:)

 

4. task시작하기

URLSessionTask인스턴스.resume()

 

if~let 사용

 

func getData(){ //1

if let url = URL(string: movieURL) { //2

let session = URLSession(configuration: .default)

let task = session.dataTask(with: url) { (data, response, error) in//3

if error != nil {

print(error!)

return

}

if let JSONdata = data { //4

let dataString = String(data: JSONdata, encoding: .utf8)

print(dataString!)

let decoder = JSONDecoder()

do{ //5

let decodedData=try decoder.decode(MovieData.self,from:JSONdata)

self.movieData = decodedData

DispatchQueue.main.async {

self.table.reloadData()

}

}catch{

print(error)

} //5

} //4

} //3

task.resume()

} //2

} //1

 

 

guard~let 사용

 

func getData(){ //1

guard let url = URL(string: movieURL) else { return }

let session = URLSession(configuration: .default)

let task = session.dataTask(with: url) {(data, response, error) in //2

if error != nil {

print(error!)

return

}

guard let JSONdata = data else { return }

let dataString = String(data: JSONdata, encoding : .utf8)

print(dataString!)

let decoder = JSONDecoder()

do{

let decodedData = try decoder.decode(MovieData.self, from: JSONdata)

self.movieData = decodedData

DispatchQueue.main.async{

self.table.reloadData()

}

}catch {

print(error)

}

} //2

task.resume()

} //1

 

후행 클로저 예

 

let onAction = UIAlertAction(title: "On", style:

UIAlertAction.Style.default) {

ACTION in self.lampImg.image = self.imgOn

self.isLampOn=true

}

 

let removeAction = UIAlertAction(title: "제거", style:

UIAlertAction.Style.destructive, handler: {

ACTION in self.lampImg.image = self.imgRemove

self.isLampOn=false

})

 

후행 클로저

 

디폴트 매개변수(아규먼트) 정의하기

 

argument로 전달하는 값이 없는 경우, 디폴트 매개변수 값을 사용

함수를 선언할 때 매개변수에 디폴트 값을 할당

이름이 인자로 전달되지 않을 경우에 디폴트로 "길동"이라는 문자열이

사용되도록 함

 

func sayHello(count: Int, name: String = "길동") -> String {

return ("\(name) 번호는 \(count)")

}

var message = sayHello(count:10, name: "철수")

print(message) //철수 번호는 10

message = sayHello(count:100) //name에 값을 전달하지 않음

print(message) //길동 번호는 100