【问题标题】:Error: Use of self in property access 'reuseidentifier' before super.init initializes self错误:在 super.init 初始化 self 之前,在属性访问“reuseidentifier”中使用 self
【发布时间】:2015-03-08 18:53:31
【问题描述】:

您好,我是 IOS 编程新手,正在尝试了解以下错误实际上告诉我什么以及如何修复它。有人可以帮忙吗?

下面添加了我的 TableViewCell.swift 文件中的代码。

import UIKit

class SweetTableViewCell: UITableViewCell {

    @IBOutlet  var usernameLabel: UILabel! = UILabel()
    @IBOutlet  var timestampLabel: UILabel! = UILabel()
    @IBOutlet  var profileImageView: UIImageView! = UIImageView()
    @IBOutlet  var sweetTextView: UITextView! = UITextView()

    required init(coder aDecoder: (NSCoder!)) {
        super.init(coder: aDecoder)
    }


    override init(style: UITableViewCellStyle, reuseIdentifier reuseIdenifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // Initialization code
    }
}

【问题讨论】:

  • 您能否修复您的代码格式并在您的问题正文中显示您收到的错误消息?

标签: ios swift


【解决方案1】:

打错了,t 不见了:

reuseIdenifier
        ^^

所以在初始化体中你实际上是在引用实例属性,这就是错误的状态。

但由于外部名称和本地名称相同,最好使用# 快捷方式:

override init(style: UITableViewCellStyle, # reuseIdentifier: String?) {

【讨论】:

    【解决方案2】:

    令人困惑的错误消息,但我认为问题在于您的“reuseIdenifier”本地参数名称。

    试试:

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    

    不幸的是,我无法进行深层链接,只能查看 Swift 文档中的外部参数名称部分:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

    它给出这个错误的原因是因为当你使用reuseIdentifier时你真的在你的init中访问self.reuseIdentifier,因为reuseIdentifier实际上是那个范围内的参数(由于本地参数名称)。在 self 初始化之前,您无法访问 self 的属性。

    【讨论】:

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