- This topic has 0개 답변, 1명 참여, and was last updated 2 years, 4 months 전에 by ygc1577.
-
글쓴이글
-
-
ygc1577참가자
- 글작성 : 9
- 답글작성 : 5
원래 파일이
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var promises = Promise{
didSet {
self.savePromiseList()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.configureTableView()
self.loadPromiseList()
}
private func configureTableView(){
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let WriteCellViewController = segue.destination as? WriteCellViewController {
WriteCellViewController.delegate = self
}
}
private func savePromiseList(){
let date = self.promises.map {
[
“title”: $0.title,
“detailcontent”: $0.detailcontent,
“datetime”: $0.datetime,
“location”: $0.location,
“isStar”: $0.isStar
]
}
let userDefaults = UserDefaults.standard
userDefaults.set(date, forKey: “promises”)
}
private func loadPromiseList(){
let userDefaults = UserDefaults.standard
guard let data = userDefaults.object(forKey: “promises”) as? [[String: Any]] else { return }
self.promises = data.compactMap{
guard let title = $0[“title”] as? String else { return nil }
guard let detailcontent = $0[“detailcontent”] as? String else { return nil }
guard let date = $0[“datetime”] as? Date else { return nil }
guard let location = $0[“location”] as? MKMapItem else { return nil }
guard let isStar = $0[“isStar”] as? Bool else { return nil }
return Promise(title: title, detailcontent: detailcontent, datetime: date, location: location, isStar: isStar)
}
}
private func dateToString(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = “yy년 MM월 dd일(EEEEE)”
formatter.locale = Locale(identifier: “ko-KR”)
return formatter.string(from: date)
}
}
extension ViewController: UITableViewDelegate {
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.promises.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let customcell = tableView.dequeueReusableCell(withIdentifier: “Cell”, for: indexPath) as? CustomCell else {return UITableViewCell() }
let promise = self.promises[indexPath.row]
customcell.title.text = promise.title
customcell.location.text = promise.location.name
customcell.datetime.text = self.dateToString(date: promise.datetime)
return customcell
}
}
extension ViewController: WriteCellViewDelegate {
func didSelectRegister(promise: Promise) {
self.promises.append(promise)
self.tableView.reloadData()
}
}
이녀석 입니다. 즉 다음 화면인 tableview cell 입력란에서 정보를 받아와
이를 현재 화면에 Cell로 나타내는 형태인데
custom class 배열의 요소로 location 변수가 MKMapItem 타입이라서 제대로 구동하지 않습니다
그래서 NSkeyedUnArchiever를 써야한다는 건 알겠는데
이를 어떤 형태로 지금의 코드에 적용해야 할지 모르겠습니다
아래는 커스텀 클래스 파일입니다
// Promise.swift
// waudiya
//
// Created by mac on 2022/08/06.
//
import Foundation
import MapKit
class Promise: NSObject, NSCoding {
var title: String
var detailcontent: String
var datetime: Date
var location: MKMapItem
var isStar: Bool
init(title aTitle : String, detailcontent aDetailcontent : String, datetime aDatetime : Date, location aLocation : MKMapItem, isStar aIsstar : Bool) {
self.title = aTitle
self.detailcontent = aDetailcontent
self.datetime = aDatetime
self.location = aLocation
self.isStar = aIsstar
}
//NSCoding Protocol 구현
required init?(coder: NSCoder) {
self.title = coder.decodeObject(forKey: “title”) as! String
self.detailcontent = coder.decodeObject(forKey: “detailcontent”) as! String
self.datetime = coder.decodeObject(forKey: “datetime”) as! Date
self.location = coder.decodeObject(forKey: “location”) as! MKMapItem
self.isStar = coder.decodeObject(forKey: “isStar”) as! Bool
}
func encode(with coder: NSCoder) {
coder.encode(self.title, forKey: “title”)
coder.encode(self.detailcontent, forKey: “detailcontent”)
coder.encode(self.datetime, forKey: “datetime”)
coder.encode(self.location, forKey: “location”)
coder.encode(self.isStar, forKey: “isStar”)
}
}
제가 어색하게 나마 고쳐보았지만 Thread 1: signal SIGABRT 오류가 뜨며 계속 멈추네요
도대체 어떻게 해야 할까요?
제발 도와주세요
2022-08-15 오후 3:05 #55442
-
-
글쓴이글
- 답변은 로그인 후 가능합니다.