我想编写一个具有 2 个计算属性的类,其中第二个属性引用第一个属性。我试过这个:
class GraphView: UIView {
var graphVisibleSize: CGSize {
return CGSize(
width: self.bounds.size.width / 40,
height: self.bounds.size.height / 40)
}
var graphRect = CGRect {
return CGRect(
center: CGPointZero,
size: self.graphVisibleSize)
}
}
但是,这会导致错误。 Xcode 在 size: self.graphVisibleSize
上给出错误行并说:
'GraphView -> () -> GraphView' does not have a member named 'graphVisibleSize'
这是什么意思?具体来说:
GraphView -> () -> GraphView
是什么意思?部分意思是?为什么看不到
graphVisibleSize
就在它上面的成员?额外问题:我尝试在测试类中复制此内容,最终编写了一个运行良好的类 - 但我不明白有什么不同!这是我作为测试而编写的:
func mungeString(str: String) -> String { var newString = "--asdf--\(str)--qwer--" return newString } class TestClass: UIView { var baseA: String = "This is a string" var baseB: String { return "This is a computed string" } var changedA: String { return self.baseA.lowercaseString } var changedB: String { return mungeString(self.baseB) } }
为什么这段代码工作正常,但原始代码却出现错误?
最佳答案
问题是由这一行引起的
var graphRect = CGRect {
应该是
var graphRect: CGRect {
像其他var
一样
关于swift - 错误“"' ClassName -> () -> ClassName'没有名为 'memberName' 的成员”是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32131795/