错误:使用' self'在财产访问'内容'在self.init初始化自我之前

时间:2017-01-25 14:34:11

标签: ios swift xcode

我已经看到了关键字Use of 'self' in property的所有问题 但仍然无法解决它。我刚开始学习swift 3.01,我的Xcode版本是8.2。请帮帮我!

我正在改变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
  }
}

请帮助我找到那里的错误,最好的关注!

2 个答案:

答案 0 :(得分:2)

您还需要解码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)
}

顺便说一下:guard / encode方法中的decode值会显示开发人员/设计错误。除了应用程序之外,没有其他人可以创建这些对象,您应该知道该值确实存在。您不能guard来捕获实际上永远不会发生的运行时错误。

答案 1 :(得分:0)

我已在let content = content.text MealViewController func prepare

中解决了我的问题添加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!) }

完全看起来像这样:

{{1}}

只要让每个人都参与其中。希望你会喜欢。