キーワード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
をデコードする必要が
少なくとも、あなたの 'init?(coder aDecoder:NSCoder)'は 'content'を初期化せずに参照しています。コンパイラがあなたが 'self.content'を参照していると思っているので、指定されたinitに渡すローカルな値を持っていたのですか? – Grimxn
あなたの応答と英語の文法の間違いを変更していただきありがとうございます。 私はおそらくそれについて知っています。 –