为什么类型 'UIView' 的值没有来自引用 UIView 变量的成员?
Why value of type 'UIView' has no member from reference UIView variable?
我想将变量引用到特定函数。
但是,有一个名为 Value of type 'UIView' has no member 'lineTo'
的错误
显然,whatSelectObject
变量包含成员所在的 类。
所以我用了If语句,"optional binding."但是结果是一样的
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class ObjectView: UIView {
var outGoingLine : CAShapeLayer?
var inComingLine : CAShapeLayer?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func lineTo(connectToObj: ObjectView) -> CAShapeLayer {
let path = UIBezierPath()
path.move(to: CGPoint(x: self.frame.maxX, y: self.frame.midY))
path.addLine(to: CGPoint(x: connectToObj.frame.minX, y: connectToObj.frame.midY))
let line = CAShapeLayer()
line.path = path.cgPath
line.lineWidth = 5
line.fillColor = UIColor.clear.cgColor
line.strokeColor = UIColor.gray.cgColor
connectToObj.inComingLine = line
outGoingLine = line
return line
}
}
class MyViewController : UIViewController {
var whatSelectView: UIView?
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let object = ObjectView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
object.backgroundColor = UIColor.orange
view.addSubview(object)
whatSelectView = object
let object2 = ObjectView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
object2.backgroundColor = UIColor.red
view.addSubview(object2)
if let s = whatSelectView {
view.layer.addSublayer(s.lineTo(connectToObj: object2)) // error
}
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
!此代码是问题代码的简化示例,以帮助受访者理解它。
我可以直接引用 object
变量,但我必须引用 whatSelectView
不能失败。
为什么会导致引用变量出错?还是我的 Optional 绑定有误?
if let s = whatSelectView as? ObjectView {
view.layer.addSublayer(s.lineTo(connectToObj: object2))
}
这应该可以解决您的问题。
改变
var whatSelectView: UIView?
到
var whatSelectView: ObjectView?
它会编译。正如@Larme 所说,当您将其声明为简单的 UIView
时,您将无法访问 UIView
的 子类 上的任何成员和函数。
确实 UIView 没有任何成员或方法称为 lineTo
,您必须使用转换(在您的情况下为 as?
)
像这样:
if let s = whatSelectView as? ObjectView {
view.layer.addSublayer(s.lineTo(connectToObj: object2)) // error
}
除此之外,对 whatSelectView
的引用应该是弱的,因为视图已经保持对它的强引用,因为它的对象是一个子视图。
而且您根本不需要 if 条件,您已经使用对象引用了视图。
所以,这是我关于更好实施的建议
class MyViewController : UIViewController {
weak var whatSelectView: ObjectView?
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let object = ObjectView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
object.backgroundColor = UIColor.orange
view.addSubview(object)
whatSelectView = object
let object2 = ObjectView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
object2.backgroundColor = UIColor.red
view.addSubview(object2)
let shape = object.lineTo(connectToObj: object2)
view.layer.addSublayer(shape)
self.view = view
}
}
我想将变量引用到特定函数。
但是,有一个名为 Value of type 'UIView' has no member 'lineTo'
的错误
显然,whatSelectObject
变量包含成员所在的 类。
所以我用了If语句,"optional binding."但是结果是一样的
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class ObjectView: UIView {
var outGoingLine : CAShapeLayer?
var inComingLine : CAShapeLayer?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func lineTo(connectToObj: ObjectView) -> CAShapeLayer {
let path = UIBezierPath()
path.move(to: CGPoint(x: self.frame.maxX, y: self.frame.midY))
path.addLine(to: CGPoint(x: connectToObj.frame.minX, y: connectToObj.frame.midY))
let line = CAShapeLayer()
line.path = path.cgPath
line.lineWidth = 5
line.fillColor = UIColor.clear.cgColor
line.strokeColor = UIColor.gray.cgColor
connectToObj.inComingLine = line
outGoingLine = line
return line
}
}
class MyViewController : UIViewController {
var whatSelectView: UIView?
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let object = ObjectView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
object.backgroundColor = UIColor.orange
view.addSubview(object)
whatSelectView = object
let object2 = ObjectView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
object2.backgroundColor = UIColor.red
view.addSubview(object2)
if let s = whatSelectView {
view.layer.addSublayer(s.lineTo(connectToObj: object2)) // error
}
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
!此代码是问题代码的简化示例,以帮助受访者理解它。
我可以直接引用 object
变量,但我必须引用 whatSelectView
不能失败。
为什么会导致引用变量出错?还是我的 Optional 绑定有误?
if let s = whatSelectView as? ObjectView {
view.layer.addSublayer(s.lineTo(connectToObj: object2))
}
这应该可以解决您的问题。
改变
var whatSelectView: UIView?
到
var whatSelectView: ObjectView?
它会编译。正如@Larme 所说,当您将其声明为简单的 UIView
时,您将无法访问 UIView
的 子类 上的任何成员和函数。
确实 UIView 没有任何成员或方法称为 lineTo
,您必须使用转换(在您的情况下为 as?
)
像这样:
if let s = whatSelectView as? ObjectView {
view.layer.addSublayer(s.lineTo(connectToObj: object2)) // error
}
除此之外,对 whatSelectView
的引用应该是弱的,因为视图已经保持对它的强引用,因为它的对象是一个子视图。
而且您根本不需要 if 条件,您已经使用对象引用了视图。
所以,这是我关于更好实施的建议
class MyViewController : UIViewController {
weak var whatSelectView: ObjectView?
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let object = ObjectView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
object.backgroundColor = UIColor.orange
view.addSubview(object)
whatSelectView = object
let object2 = ObjectView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
object2.backgroundColor = UIColor.red
view.addSubview(object2)
let shape = object.lineTo(connectToObj: object2)
view.layer.addSublayer(shape)
self.view = view
}
}