Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中,计算属性和存储值属性之间有什么区别? 问题:_Swift_Xcode_Properties - Fatal编程技术网

在Swift中,计算属性和存储值属性之间有什么区别? 问题:

在Swift中,计算属性和存储值属性之间有什么区别? 问题:,swift,xcode,properties,Swift,Xcode,Properties,行中的两行之间有什么区别。swift 背景: 行。swift open class Row { // Existing code (Good): public var cellIdentifier: String { return String(describing self) } // The change (Bad): public var cellIdentifier: String = String(describing: self) public class Di

行中的两行之间有什么区别。swift

背景: 行。swift

open class Row {

  // Existing code (Good):
  public var cellIdentifier: String { return String(describing self) }

  // The change (Bad):
  public var cellIdentifier: String = String(describing: self)
public class DifferentRow: Row {

  public override var cellIdentifier: String { return "\(super.cellIdentifier)" }
  // returns the error below
DIFFERTrow.swift

open class Row {

  // Existing code (Good):
  public var cellIdentifier: String { return String(describing self) }

  // The change (Bad):
  public var cellIdentifier: String = String(describing: self)
public class DifferentRow: Row {

  public override var cellIdentifier: String { return "\(super.cellIdentifier)" }
  // returns the error below
错误: 无法使用只读属性“cellIdentifier”重写可变属性,请执行以下操作:

public var cellIdentifier: String { return String(describing self) }
正在定义计算属性。不存储任何值。每次访问
cellIdentifier
时,闭包都会运行并返回
字符串。它是只读的,因为只提供了getter

这:

是一个存储值属性,它是读/写的

错误告诉您不能将具有读/写功能的属性替换为仅具有读功能的属性

注意:如果使用值初始化属性,则无法访问
self
,因为
self
在对象完全初始化之前并不表示类/结构实例。如果将属性设置为
lazy var
,则可以在初始化中使用
self
,因为这样一来,属性在第一次访问时就会初始化

您可以阅读有关Swift属性的更多信息

如下:

public var cellIdentifier: String { return String(describing self) }
正在定义计算属性。不存储任何值。每次访问
cellIdentifier
时,闭包都会运行并返回
字符串。它是只读的,因为只提供了getter

这:

是一个存储值属性,它是读/写的

错误告诉您不能将具有读/写功能的属性替换为仅具有读功能的属性

注意:如果使用值初始化属性,则无法访问
self
,因为
self
在对象完全初始化之前并不表示类/结构实例。如果将属性设置为
lazy var
,则可以在初始化中使用
self
,因为这样一来,属性在第一次访问时就会初始化


您可以阅读有关Swift属性的更多信息

您不能使用只能读取的属性覆盖“读取和写入”属性

您可以指定不同的值:

public override var cellIdentifier: String = “newValue”
或者创建一个set和一个get实现:

public override var cellIdentifier: String {
    get { return “\(super.cellIdentifier)” }
    set { super.cellIdentifier = newValue }
}

计算属性下
阅读更多信息。

您不能用只能读取的属性覆盖“读取和写入”属性

您可以指定不同的值:

public override var cellIdentifier: String = “newValue”
或者创建一个set和一个get实现:

public override var cellIdentifier: String {
    get { return “\(super.cellIdentifier)” }
    set { super.cellIdentifier = newValue }
}

计算属性下
阅读更多相关信息。

我认为错误消息相当混乱

问题在于

public var cellIdentifier: String = String(describing: self)
是对
self
的引用:

  • 在定义过程中分配
    cellIdentifier
    时,
    self
    不保证完全初始化
  • 因此,禁止调用函数(如
    String(description:)
    )并交上半初始化的
    self
一种解决方案是将
cellIdentifier
设置为惰性属性:

public lazy var cellIdentifier: String = String(describing: self)

这将自动将函数调用延迟到初始化完成后的时间。

我认为错误消息非常混乱

问题在于

public var cellIdentifier: String = String(describing: self)
是对
self
的引用:

  • 在定义过程中分配
    cellIdentifier
    时,
    self
    不保证完全初始化
  • 因此,禁止调用函数(如
    String(description:)
    )并交上半初始化的
    self
一种解决方案是将
cellIdentifier
设置为惰性属性:

public lazy var cellIdentifier: String = String(describing: self)

这将自动将函数调用延迟到初始化完成后的时间。

此代码在文件中的位置?给我们一些上下文。@RakeshaShastri Edited此代码在文件中的位置?给我们一些上下文。@RakeshaShastri editedAlso self不能在方法或闭包之外访问,对吗?你能解释一下吗?在方法或闭包之外也不能访问self,对吗?你能解释一下那部分吗?