2017-01-25 6 views
1

キーワードUse of 'self' in property のすべての問題を見ましたが、解決できませんでした。私はちょうど早い3.01を学び始めました、そして、私のXcodeバージョンは8.2です。私を助けてください!エラー:self.initが自己を初期化する前に、プロパティアクセス 'content'で 'self'を使用してください

私はthe app Apple providedを変更していました。

textViewを作成して、ユーザーがテキストを入力して保存できるようにしたいとします。 私はMeal.swiftを変えましたが、私はエラーを得た:ちょうどコードの末尾に Error :Use of 'self' in property access 'content' before self.init initializes self

import UIKit 
import os.log 

class Meal: NSObject, NSCoding { 

    //MARK: Properties 
    var name: String 
    var photo: UIImage? 
    var content: String  

    //MARK: Archiving Paths 
    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first! 
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("meals") 

    //MARK: Types 
    struct PropertyKey { 
    static let name = "name" 
    static let photo = "photo" 
    static let content = "content" 
    } 

    //MARK: Initialization 
    init?(name: String, photo: UIImage?, content: String) { 
    // The name must not be empty 
    guard !name.isEmpty else { 
     return nil 
    } 

    // The content must not be empty 
    guard !content.isEmpty else { 
     return nil 
    } 

    // Initialize stored properties. 
    self.name = name 
    self.photo = photo 
    self.content = content 
    } 

    //MARK: NSCoding 
    func encode(with aCoder: NSCoder) { 
    aCoder.encode(name, forKey: PropertyKey.name) 
    aCoder.encode(photo, forKey: PropertyKey.photo) 
    aCoder.encode(content, forKey: PropertyKey.content) 
    } 

    required convenience init?(coder aDecoder: NSCoder) { 
    // The name is required. If we cannot decode a name string, the initializer should fail. 
    guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else { 
     os_log("Unable to decode the name for a Meal object.", log: OSLog.default, type: .debug) 
     return nil 
    } 

    // Because photo is an optional property of Meal, just use conditional cast. 
    let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage 

    // Must call designated initializer. 
    self.init(name: name, photo: photo, content: content)// Error happened to here by this : Error :Use of 'self' in property access 'content' before self.init initializes self 
    } 
} 

そこに何が間違って、最高点を見つけるために私を助けてください!あなたがところで、あまりにも、

required convenience init?(coder aDecoder: NSCoder) { 

    // The name is required. If we cannot decode a name string, the initializer should fail. 
    let name = aDecoder.decodeObject(forKey: PropertyKey.name) as! String 

    // Because photo is an optional property of Meal, just use conditional cast. 
    let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage 

    let content = aDecoder.decodeObject(forKey: PropertyKey.content) as! String 
    // Must call designated initializer.   
    self.init(name: name, photo: photo, content: content) 
} 

contentをデコードする必要が

+1

少なくとも、あなたの 'init?(coder aDecoder:NSCoder)'は 'content'を初期化せずに参照しています。コンパイラがあなたが 'self.content'を参照していると思っているので、指定されたinitに渡すローカルな値を持っていたのですか? – Grimxn

+0

あなたの応答と英語の文法の間違いを変更していただきありがとうございます。 私はおそらくそれについて知っています。 –

答えて

2

guard ING値をencode/decode方法では、開発/設計ミスを明らかにする。誰もアプリケーションを作成することはできませんが、その値が存在することを知っているはずです。実際には発生しない実行時エラーをキャッチするには、guardを使用しないでください。

+0

それは私がAppが動くことができる別の問題を持っていますが、textViewにテキストを保存させることはできません。 私は問題がNSUserDefaultによって修正される可能性があることを知っていますが、実際にどのように使用するのか理解できません。私はどこに私が使用する方法を見つけることができるか教えてくれますか?どうもありがとう! –

+0

申し訳ありませんが、あなたのコードに関連するテキストビューはありません。より多くの情報とこれまでのものを提供する新しい質問をしてください。 – vadian

+0

さて、わかりました。ありがとうございました。 –

0

私は私の問題は、全くそのように見えますMealViewControllerさんfunc prepare

let content = content.textを追加し解決しました:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    super.prepare(for: segue, sender: sender) 

    // Configure the destination view controller only when the save button is pressed. 
    guard let button = sender as? UIBarButtonItem, button === saveButton else { 
     os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug) 
     return 
    } 

    let name = nameTextField.text ?? "" 
    let photo = photoImageView.image 
    let content = content.text // Add this new code. 

    // Set the meal to be passed to MealTableViewController after the unwind segue. 
    meal = Meal(name: name, photo: photo, content: content!) 

} 

だけreferanceように皆を与えます。あなたが楽しむことを願っています。

関連する問題