그래서 문제가 있습니다. 나는 현재 "코스"에 대한 정보를 저장하는이 앱을 만들고 있습니다. 그러나, 나는 앱을 많이 바 꾸었습니다. 이제는 코스 사전을 원합니다. 나는이 과목 사전을 저장하고 어떤 수업에서라도 불러올 수 있어야한다.NSCoding 스위프트를 사용하여 사용자 지정 개체 배열 사전을 저장하는 방법
현재 Course.swift에 NSCoding 설정이 있습니다. 내 프로그램은 모든 코스 정보를 쓰고 읽습니다. 하지만 이제는 모든 과정 대신이 사전을 쓰도록 변경하고 싶습니다. 이 사전을 보유하고있는 또 다른 데이터 클래스가 없으며 단지 "StartUpViewController.swift"에 보관되어 있습니다.
class Course: NSObject, NSCoding {
// MARK: Properties
var courseName : String
var projects : [String]
var projectMarks : [Double]
var projectOutOf : [Double]
var projectWeights : [Double]
// MARK: Archiving Paths
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("courses")
// MARK: Types
struct PropertyKey {
static let courseNameKey = "courseName"
static let projectsKey = "projects"
static let projectMarksKey = "projectMarks"
static let projectOutOfKey = "projectOutOf"
static let projectWeightsKey = "projectWeights"
}
// MARK: NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(courseName, forKey: PropertyKey.courseNameKey)
aCoder.encode(projects, forKey: PropertyKey.projectsKey)
aCoder.encode(projectMarks, forKey: PropertyKey.projectMarksKey)
aCoder.encode(projectOutOf, forKey: PropertyKey.projectOutOfKey)
aCoder.encode(projectWeights, forKey: PropertyKey.projectWeightsKey)
}
required convenience init?(coder aDecoder: NSCoder) {
let courseName = aDecoder.decodeObject(forKey: PropertyKey.courseNameKey) as! String
let projects = aDecoder.decodeObject(forKey: PropertyKey.projectsKey) as! [String]
let projectMarks = aDecoder.decodeObject(forKey: PropertyKey.projectMarksKey) as! [Double]
let projectOutOf = aDecoder.decodeObject(forKey: PropertyKey.projectOutOfKey) as! [Double]
let projectWeights = aDecoder.decodeObject(forKey: PropertyKey.projectWeightsKey) as! [Double]
self.init(courseName: courseName, projects: projects, projectMarks: projectMarks, projectOutOf: projectOutOf, projectWeights: projectWeights)
}
어떻게해야합니까? NSCoding을 사용하여 Course.swift를 유지합니까, 아니면 View Controller에 NSCoding 만 넣어야합니까?
class StartUpViewController: UIViewController {
var groups: [String: [Course]?] = [:]
...
}
답장을 보내 주셔서 감사합니다. 질문이 하나 더 있습니다. 이제 모든 것을 저장하려고하면 "그룹"만 저장해야합니까, 아니면 모든 코스를 저장하라는 전화를해야합니까? – Logan
그룹을 저장하면 코스가 자동으로 재귀 적으로 저장됩니다. –