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프로그래밍 실무 6주차 정리 본문

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

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

김브래 2023. 4. 12. 18:23

오늘은 처음으로 스위프트 문법 중 옵셔널 체이닝 이라는 것을 배웠다.

자세한 내용은 추후에 클래스 카테고리의 스위프트 카테고리에 따로 글을 작성하겠다.

 

일단 옵셔널 체이닝이란 옵셔널을 언래핑하는 방법 중 하나 이다.

옵셔널을 언래핑 하는 방법은 원래 3가지를 알고 있었다.

 

1. !를 이용한 옵셔널 강제 언래핑

 

- 간단하게 언래핑을 할 수 있지만 변수 안에 nil값이 들어가 있다면 크래쉬가 나므로 위험한 방법이다.

 

 

2. if~let, guard~let을 이용한 옵셔널 바인딩

 

- nil이 저장되어 있는 경우를 설정할 수 있기 때문에 대표적으로 사용되는 언래핑 방법이다.

 

3. nil 합병 연산자 ??

let optionalValue: Int? = nil

let defaultValue: Int = 10

let result = optionalValue ?? defaultValue

print(result) // 출력: 10

이 방법도 옵셔널 변수가 nil이 아니라면 언래핑 되고 

nil이라면 ?? 뒤의값이 나오므로 안전한 방법이다.

그리고 이번에 배운 옵셔널 체이닝이 네번째 방법인데

변수 뒤에 ?를 붙이는 거다.

 

소스 예제

var x : String? = "Hi"
print(x, x!)
if let a = x { // 옵셔널 바인딩
print(a)
}
let b = x!.count // 만약 x에 nil이 들어있다면 크래쉬, 위험한 방법
print(type(of:b),b) // Int, 2
let b1 = x?.count // x다음에 있는 ?가 옵셔널 체이닝, x에 nil 값이 들어 있을 수도 있으니 b1을 옵셔널 형으로 반환
print(type(of:b1),b1, b1!)// Optional Int, Optional 2, 2
let c = x ?? ""// nil 합병 연산자
print(c)

 

그리고 나머지 시간에는 맥의

Xcode로 TableView를 만드는 실습을 했다.

 

자세한 설명은 클래스 카테고리의 스위프트에 나와 있으니 참고 바란다.

 

- 테이블 뷰 소스 -

 

import UIKit

 

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 20

    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 3

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "myCell")

        cell.textLabel?.text = "\(indexPath.row)"

        cell.detailTextLabel?.text = indexPath.description

        cell.imageView?.image = UIImage(named: "plan.png")

        return cell

    }

    

 

    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {

        super.viewDidLoad()

        table.delegate = self

        table.dataSource = self

    }

 

}

—————————————————————————————

import UIKit

 

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 20

    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 3

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell.init(style: .default, reuseIdentifier: "myCell")

        cell.textLabel?.text = "\(indexPath.row)"

        cell.detailTextLabel?.text = indexPath.description

        cell.imageView?.image = UIImage(named: "plan.png")

        return cell

    }

    

 

    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {

        super.viewDidLoad()

        table.delegate = self

        table.dataSource = self

    }

 

}

——————————————————————————————

import UIKit

 

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 20

    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 3

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell.init(style: .value1, reuseIdentifier: "myCell")

        cell.textLabel?.text = "\(indexPath.row)"

        cell.detailTextLabel?.text = indexPath.description

        cell.imageView?.image = UIImage(named: "plan.png")

        return cell

    }

    

 

    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {

        super.viewDidLoad()

        table.delegate = self

        table.dataSource = self

    }

 

}