protocol NavigationUIProtocol {
var titleString: String { get set }
var mainTintColor: UIColor { get }
}
class JackViewController: UIViewController, NavigationUIProtocol {
var titleString: String = "나의 일기"
var mainTintColor: UIColor = .black // get만 가능하다고 했는데 왜 set도 가능? -> get만 가능하다고 한 경우, get은 필수인데 set은 선택. 그니까 구현해도 ㄱㅊ음!!
override func viewDidLoad() {
super.viewDidLoad()
title = titleString
view.backgroundColor = mainTintColor
}
}
class Jack2ViewController: UIViewController, NavigationUIProtocol {
var titleString: String {
get {
return "나의 일기"
}
set {
title = newValue
}
}
var mainTintColor: UIColor {
get {
return .black
}
set {
view.backgroundColor = newValue
}
}
override func viewDidLoad() {
super.viewDidLoad()
titleString = "새로운 일기"
}
}
protocol OrderSystem {
func recommendEventMenu()
func askStampcard(count: Int) -> String
}
class StarBucksMenu {
}
class Smoothie: StarBucksMenu, OrderSystem {
func recommendEventMenu() {
print("스무디 이벤트 안내")
}
func askStampcard(count: Int) -> String {
return "\(count)잔 적립 완료"
}
}
class Coffee: StarBucksMenu, OrderSystem {
func recommendEventMenu() {
print("커피 이벤트 안내")
}
func askStampcard(count: Int) -> String {
return "\(count * 2)잔 적립 완료"
}
}
optional을 사용하려면 반드시 @objc
를 작성해줘야한다.
@objc
protocol 프로토콜이름 {
@objc optioanl func 메서드이름{}
}
protocol OrderSystem {
func recommendEventMenu()
init()
}
class Smoothie: StarBucksMenu, OrderSystem {
func recommendEventMenu() {
print("스무디 이벤트 안내")
}
// 부모에서도 init이 있을 수 있기 때문에 구별을 위해서 required를 붙임
required init() {
}
}
프로토콜을 클래스에서만 사용하게 제한하고 싶은 경우에는 AnyObject
를 상속해주면 된다. 해당 키워드가 작성된 프로토콜은 Struct에서 사용할 수 없다.
protocol OrderSystem: AnyObject {
func recommendEventMenu()
}
enum SettingSection: Int, CaseIterable {
case authorization
case onlineShop
case question
var description: String {
switch self {
case .authorization:
return "알림 설정"
case .onlineShop:
return "온라인 스토어"
case .question:
return "Q&A"
}
}
}
class SettingViewController: UIViewController {
@IBOutlet var setttingTableView: UITableView!
let settingList = [
["위치 알림 설정", "카메라 알림 설정"],
["교보 문고", "영풍 문고", "반디앤루니스"],
["앱스토어 리뷰 작성하기", "문의하기"]
]
override func viewDidLoad() {
super.viewDidLoad()
setttingTableView.delegate = self
setttingTableView.dataSource = self
let nibName = UINib(nibName: DefaultTableViewCell.identifier, bundle: nil)
setttingTableView.register(nibName, forCellReuseIdentifier: DefaultTableViewCell.identifier)
}
}
extension SettingViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
// CaseIterable 프로토콜 사용해서 가능한 코드!
return SettingSection.allCases.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return settingList[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: DefaultTableViewCell.identifier, for: indexPath) as? DefaultTableViewCell else {
return UITableViewCell()
}
cell.iconImageView.backgroundColor = .blue
cell.titleLabel.text = settingList[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return SettingSection.allCases[section].description
}
}
var movieData: Movie?
titleTextField.text = movieData?.title
overviewTextView.text = movieData?.overview
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let sb = UIStoryboard(name: "Movie", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "detailBoxOfficeViewController") as! detailBoxOfficeViewController
let data = movieInformation.movie[indexPath.row]
vc.movieData = data <<
self.navigationController?.pushViewController(vc, animated: true)
}
➡️ 이 방식은 넘어간 화면에서 다시 이전 화면으로 돌아갈 때는 값을 전달하기 어렵다는 단점이 있음
Also create XIB file
에 체크하면 XIB 파일을 생성할 수 있다.class DefaultTableViewCell: UITableViewCell {
static let identifier = "DefaultTableViewCell"
@IBOutlet var iconImageView: UIImageView!
@IBOutlet var titleLabel: UILabel!
}
static let identifier = "DefaultTableViewCell"
다음과 같이 코드를 작성해주면 tableViewController에서 identifier를 직접 작성하는 것이 아니라 tableViewCell 클래스 내의 프로퍼티를 활용하여 적용해줄 수 있다.let nibName = UINib(nibName: DefaultTableViewCell.identifier, bundle: nil)
setttingTableView.register(nibName, forCellReuseIdentifier: DefaultTableViewCell.identifier)
요청 ➡️ Struct ➡️ Data ➡️ Plist
순으로 진행된다.NSKeyedArchiver
를 사용하면 된다고 한다.(추후 학습 예정)