# To do Step 1-1
---
- [x] 0. RootViewController -> NavigationController 삽입
- [x] 1. UI
- [x] - 1. 테이블뷰
- [x] - 2. 커스텀 셀
- [x] - 3. 어트리뷰티드 스트링 이용 -> 레이블 생성
- [x] - 1. 스택 뷰
- [x] - 1. 일반 제목 레이블, 어트리뷰티드 레이블 -> 묶음
- [x] - 2. 악세사리 - 디스클로저
- [x] - 4. navigationTitle -> 일기장
- [x] - 5. navigationBarbutton -> + 버튼 추가
- [x] - 6. 작성일자 -> 사용자 지역에 맞도록
- [x] - 7. 데이트 포맷터
- [x] - 8. JSON 정보 가져오기
- [x] - 1. JSONDecoder
- [x] - 1. 셀에 데이터 넣어주기
### 코비
- 다이어리 메인 뷰컨으로 이름 바꿈
- 테이블 뷰 생성
- 커스텀 셀 추가
- 스택 뷰 생성
- 레이블 UI 생성
- 스택 뷰 UI
- Identifiable 프로토콜 생성 및 구현
- 사용자 지역에 포맷에 맞는 작성일자값 반환을 위한 DateFormatter extension 구현 및 convertDate 함수 구현
- cell : 작성자 이름, 날짜 수정
### 문
- label height 잡히지 않는 현상 fix
- DiaryTableViewCell UI
- 스택뷰 내부 레이블 간격 fix
- 네비게이션 아이템 ( + ) 추가 및 mock 액션 추가
- DTO 생성
- JSONDecoder 생성 및 구현
- Attributed String 생성
- JSON Data 가져오기
- Error 생성
# Trouble shooting
1. StackView 내부에서 Label의 Height가 잡히지 않는 현상.
(height is ambiguous for UILabel 경고)
- 해결 : https://stackoverflow.com/questions/68951886/ambiguous-uilabel-height-in-autoresizing-uitableviewcell
2. NSAttributedString이 적용되지 않는 현상.
[__NSCFConstantString renderingMode]: unrecognized selector sent to instance 0x10aa5a2e8 에러가 발생했습니다.
원인은 NSAttributedString.Key.font에 필요한 타입이 UIFont였으나 UIFont.TextStyle을 사용 중이었습니다.
```swift
private func convertAttributedString(text: String, font: UIFont.TextStyle) -> NSAttributedString {
let attributes = [NSAttributedString.Key.font: font as Any] as [NSAttributedString.Key : Any]
let attributedString = NSAttributedString(string: text, attributes: attributes)
return attributedString
}
```
- 해결 :
NSAttributedString.Key.font에 UIFont 타입으로 전달하여 해결했습니다.
```swift
private func convertAttributedString(text: String, font: UIFont) -> NSAttributedString {
let attributes = [NSAttributedString.Key.font: font as Any] as [NSAttributedString.Key : Any]
let attributedString = NSAttributedString(string: text, attributes: attributes)
return attributedString
}
```
3. NSAttributedString의 가운데 정렬
문자열 자체의 모양이 아래 이미지와 같기 때문에 UILabel의 baselineAdjustment로 가운데 정렬을 맞출 수 없었습니다.

- 해결 :
NSAttributedString.Key.baselineOffset을 이용해 작은 문자 부분의 baseline을 올리는 것으로 해결했습니다.

```swift
private func attributedDateAndPreview(data: Diary, font: UIFont) -> NSMutableAttributedString {
let text = "\(formatCreatedAt(data.createdAt)) \(data.body)"
let attributedString = NSMutableAttributedString(string: text)
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.baselineOffset: 2
]
attributedString.addAttributes(attributes, range: (text as NSString).range(of: data.body))
return attributedString
}
```
# To do Step 1-2
---
- [x] TextView 만들기
- [x] 텍스트 뷰 델리게이트 설정
- [x] 키보드 디스미스 모드 설정
- [x] 일기장 영역으로 화면 전환
### 코비
- 텍스트 뷰 생성.
### 문
- 텍스트 뷰 델리게이트 설정
- 키보드 디스미스 모드 설정
- 일기장 영역으로 화면 전환
# Trouble shooting
- keyboardLayoutGuide를 사용시 다음 그림과 같은 에러 생성git

그에 따라 Targets의 미니멍 디플로이먼츠를 15.0으로 높임
# To do Step 2
# Trouble shooting
- 코어 데이터의 영구 저장소 위치는 Application Support 디렉터리 내부여야 함
기본 저장 위치가 Application Support 디렉터리 내부임

- 백그라운드 진입 시 데이터 저장
컨텍스트만 저장할 경우 일기에서 수정 중인 내용이 반영되지 않음
윈도우를 통해 뷰 컨트롤러를 가져와 저장하는 작업이 필요할 듯하여 다음과 같이 시도 했으나 실패
```swift
func sceneDidEnterBackground(_ scene: UIScene) {
guard let diaryViewController = window?.rootViewController?.navigationController?.topViewController as? DetailDiaryViewController else {
return
}
diaryViewController.saveDiaryData()
// persistentContainer?.saveContext()
}
```
루트 뷰 컨트롤러가 네비게이션 컨트롤러였기 때문에 아래와 같이 수정
```swift
func sceneDidEnterBackground(_ scene: UIScene) {
guard let navigationController = window?.rootViewController as? UINavigationController,
let diaryViewController = navigationController.topViewController as? DetailDiaryViewController else {
return
}
diaryViewController.saveDiaryData()
}
```
- DetailDiaryViewController에서 삭제할 때 에러
2023-09-08 23:16:59.579283+0900 Diary[18142:23564874] [error] error: Mutating a managed object 0xa3d5c820a726faaa <x-coredata://155121C7-712A-4742-9703-DF8945A38D76/DiaryEntity/p10> (0x600001ce7110) after it has been removed from its context.
CoreData: error: Mutating a managed object 0xa3d5c820a726faaa <x-coredata://155121C7-712A-4742-9703-DF8945A38D76/DiaryEntity/p10> (0x600001ce7110) after it has been removed from its context.