2017-12-02 6 views
0

私のアプリでは、データの永続性を保ってデータを保存しようとしましたが、このエラーが発生します: スーパークラス 'Day'エラーの指定された初期化子を呼び出さなければなりません。ここでスーパークラス 'Day'エラーの指定されたイニシャライザを呼び出さなければなりません

が私のコードです:

class Day: NSObject, NSCoding { // NEMA NSCODING I NSOBJECT 
var dayName: String 
var subjects: [Subject]? 

init(dayName: String) { 
    self.dayName = dayName 
} 

struct PropertyKey { 
    static var dayName = "dayName" 
    static var subjects = "subjects" 
} 

func encode(with aCoder: NSCoder) { 
    aCoder.encode(dayName, forKey: PropertyKey.dayName) 
    aCoder.encode(subjects, forKey: PropertyKey.subjects) 
} 

// Archiving paths 
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first! 
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("workingDays")//-namesto meals 

required convenience init?(coder aDecoder: NSCoder) { 
    guard let dayName = aDecoder.decodeObject(forKey: PropertyKey.dayName) as? String else { 
     os_log("Unable to decode the dayName for a Day object.", log: OSLog.default, type: .debug) 
     return nil 
    } 
    let subjects = aDecoder.decodeObject(forKey: PropertyKey.subjects) as? [Subject] 
    self.init(dayName: dayName) 
} 

}

そして、ここでは他のクラスである:

class Subject: Day { 
var subjectName: String 
var startsAt: String? 
init(dayName: String,subjectName: String) { 
    self.subjectName = subjectName 
    super.init(dayName: dayName) 
} 
required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) // here I get the error 
} 

}

私はデータをこのように保存するつもりですDayクラスでのみデータ永続化を実装していますが、なぜそのエラーが発生しますか? 私は初心者です、私はこのリンゴのドキュメントに基づいて、それをやっている -

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=12&cad=rja&uact=8&ved=0ahUKEwjWoIHk4uvXAhUHaFAKHbQFCHcQFghbMAs&url=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fcontent%2Freferencelibrary%2FGettingStarted%2FDevelopiOSAppsSwift%2FPersistData.html&usg=AOvVaw24H5Uo4RyRY7NYIlztlgCj

任意の助けもいただければ幸いです。あなたのケースでは、あなたがスーパーrequiredconvenience initialiserを作っ

Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer:

You do not write the override modifier when overriding a required designated initializer:

、サブクラスは今それを実装する必要がありますので:必要なInitializers-の定義により

答えて

2

この問題は、のinit?(coderDay 。その呼び出しによってイニシャライザconvenientが作成され、サブクラスは指定された初期化子を呼び出すための要件を満たすことができません。

ソリューションは、直接

required init?(coder aDecoder: NSCoder) { 
    self.dayName = aDecoder.decodeObject(forKey: PropertyKey.dayName) as! String 
    self.subjects = aDecoder.decodeObject(forKey: PropertyKey.subjects) as? [Subject] 
} 

ところでプロパティを初期化することです:復号化されるとき、それはnilになることはできません決してので、あなたは非オプション文字列として常にdayNameをコードしています。 guardは役に立たない。

サブクラスでは、サブクラスのプロパティをエンコード/デコードするコードを追加し、superを呼び出してスーパークラスのプロパティも考慮する必要があります。

class Subject: Day { 
    var subjectName: String 
    var startsAt: String? 

    init(dayName: String, subjectName: String) { 
     self.subjectName = subjectName 
     super.init(dayName: dayName) 
    } 

    override func encode(with aCoder: NSCoder) { 
     super.encode(with: aCoder) 
     aCoder.encode(subjectName, forKey: "subjectName") 
     aCoder.encode(startsAt, forKey: "startsAt") 
    } 

    required init?(coder aDecoder: NSCoder) { 
     subjectName = aDecoder.decodeObject(forKey: "subjectName") as! String 
     startsAt = aDecoder.decodeObject(forKey: "startsAt") as? String 
     super.init(coder: aDecoder) 
    } 
} 

そのスーパークラスにプロパティとして、サブクラスを使用しての感覚のための質問は、私はそれをやってみました、私は致命的なエラー「のinit(コーダは:)実装されていない」ことを得る別の話

+0

実際には、このコードが何をするのか分かりません。これらのクラスのデータを永久に保存するか、それを行うために他のコードを書く必要がありますか?私はここでかなり新しいことに助けてくれてありがとう。 –

2

- :

コードCorrection-:

class Subject: Day { 
    var subjectName: String 
    var startsAt: String? 
    init(dayName: String,subjectName: String) { 
     self.subjectName = subjectName 
     super.init(dayName: dayName) 
    } 

    required convenience init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 


} 
+0

..です –

関連する問題