【问题标题】:Error :Use of 'self' in property access 'content' before self.init initializes self错误:在 self.init 初始化 self 之前,在属性访问“内容”中使用“自我”
【发布时间】:2017-01-25 14:34:10
【问题描述】:

我已经看到了关键字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
  }
}

请帮我找出问题所在,最好的祝福!

【问题讨论】:

  • 至少有一个问题是你的init?(coder aDecoder: NSCoder) 引用content 而没有初始化它。你的意思是有一个本地值传递给指定的init,因为编译器认为你指的是self.content...
  • 感谢您的回复,改正我在英语语法上的错误。我大概知道。

标签: ios swift xcode


【解决方案1】:

你也必须解码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 方法中的guarding 值揭示了开发人员/设计错误。除了应用程序之外没有其他人可以创建这些对象,并且您应该知道该值确实存在。你不能guard 来捕捉实际上永远不会发生的运行时错误。

【讨论】:

  • 这是我遇到的另一个问题,App 可以运行,但我不能让 textView 中的文本存储。我知道问题可能可以通过 NSUserDefault 解决,但我真的不明白如何使用它。你能告诉我或告诉我在哪里可以找到使用方法吗?非常感谢!
  • 抱歉,没有与您的代码相关的文本视图。提出一个新问题,提供更多信息以及您目前掌握的信息。
【解决方案2】:

我已经解决了我的问题,在MealViewControllerfunc 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!)

}

只是给大家做个参考。希望你会喜欢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多