![](/img/trans.png)
[英]Error: Use of self in property access 'reuseidentifier' before super.init initializes self
[英]Error :Use of 'self' in property access 'content' before self.init initializes self
我已經看到關鍵字Use of 'self' in property
所有問題,但仍然無法解決。 我剛剛開始學習Swift 3.01,而我的Xcode版本是8.2。 請幫我!
我正在更改Apple提供的應用程序 。
我想創建一個textView
以便用戶可以輸入文本並將其存儲。 當我更改Meal.swift
,出現錯誤: Error :Use of 'self' in property access 'content' before self.init initializes self
在代碼末尾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
}
}
請幫助我找到那里的問題,最好的問候!
您也必須解碼content
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)
}
順便說一句:在encode
/ decode
方法中guard
值會揭示開發人員/設計錯誤。 除了應用程序之外,沒有其他人可以創建這些對象,並且您應該知道該值確實存在。 你不能guard
趕上這實際上應該不會發生運行時錯誤。
我已經解決了我的問題,在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!)
}
只是給大家一個參考。 希望你會喜歡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.