2017-01-07 9 views
-2

私はSwift 3に入れようとしている次のコードを持っています。super.encodeWithCoder(aCoder)という文字列に問題があります。私が何をしてもエラーになります。私はスウィフト3Swift 3のエンコーディング

に変換しようとしている

import Foundation 
class ToDo: Task { 
var done: Bool 

@objc required init(coder aDecoder: NSCoder) { 
    self.done = aDecoder.decodeObjectForKey("done") as! Bool 
    super.init(coder: aDecoder) 
} 

@objc override func encodeWithCoder(aCoder: NSCoder) { 
    aCoder.encodeObject(done, forKey: "done") 
    super.encodeWithCoder(aCoder) 
} 

init(name: String, done: Bool) { 
    self.done = done 
    super.init(name: name) 
} 
} 

私はこの

import Foundation 

class ToDo: Task { 
var done: Bool 

@objc required init(coder aDecoder: NSCoder) { 
    self.done = aDecoder.decodeObject(forKey: "done") as! Bool 
    super.init(coder: aDecoder) 
} 

@objc override func encode(with aCoder: NSCoder) { 
    aCoder.encode(done, forKey: "done") 
    // THis line gives an error 
    super.encode(with aCoder) 

} 

init(name: String, done: Bool) { 
    self.done = done 
    super.init(name: name) 
} 

} 

ライン super.encodeWithCoder(aCoder) がエラーを与えています。 Swiftはプロンプトを表示せず、検索で回答が得られませんでした。

編集はコメント に応答して、元のコード「super.encodeWithCoder(aCoder)」はタイプのエラー値を与える「タスク」はメンバーを持たない(aCoder付き)「encodeWithCoder」

super.encodeエラー期待を与えます';'区切り文字

+1

エラーとは何ですか? – shallowThought

+0

奇妙なコーディングでは、これは私がこれをオーバーライドすることに慣れていない可能性が高い - 私はいつもIBで作成されたビューのみがinit(コーダー:)を使用すると考えました。それは、2つのことが私に飛びついているということです:(1)あなたはあなたがどんな種類のエラーを出すのかの詳細を投稿しませんでした。 (2)super.ecode(with:coder)を呼び出す前にいくつかのコードがあるので、ビルドエラーが発生することがありました。ボナス:私はちょうどあなたのコードがその行のコロンを欠いているのを見ました。間違いなくビルドしないと確信しています。 – dfd

+0

"' super.encodeWithCoder(aCoder) '行はエラー"を返します。その行はあなたのSwift 3の例には存在しません。 'super.encode(with aCoder)'の呼び出しがコンパイルに失敗していることを意味しますか? dfdが指摘しているように、 'super.encode(with:aCoder)'でなければならないのでコンパイルできません。 – Rob

答えて

0

私は、アプリケーションがクラッシュする理由は、BoolにdecodeObject()関数を使用することだと思います。正しい機能の利回りにコードを変更する

@objc required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    self.done = aDecoder.decodeBool(forKey: "done") 
} 

@objc override func encode(with aCoder: NSCoder) { 
aCoder.encode(done, forKey: "done") 
} 
+0

これは動作しますが、エラーメッセージが表示されないようにsuper.init(コーダー:aDecoder)を追加する必要がありました。 – cpmac

+0

それに応じて、 Haventはそれを使用していないエラーを持っていたO :) – Emptyless