問題があります。私は現在、 "コース"の情報を保存しているこのアプリを作っています。しかし、私はたくさんのアプリを変更しています。今はコースの辞書が必要です。このコースの辞書を保存して、どのクラスからでもロードできるようにする必要があります。NSCoding swiftを使用してカスタムオブジェクトの配列の辞書を保存する方法
現在、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]?] = [:]
...
}
返信いただきありがとうございます。もう1つ質問があります。今私はすべてを保存するために行くとき、私は "グループ"を保存するか、私はすべてのコースを保存する呼び出しをする必要がありますか? – Logan
グループを保存するだけで、コースが自動的に再帰的に保存されます。 –