Swift Instance成员不能用于类型

Swift Instance成员不能用于类型,第1张

概述我在超类中定义了一个变量并尝试在子类中引用它,但是在实例成员上获取错误不能在类型上使用 class supClass: UIView { let defaultFontSize: CGFloat = 12.0}class subClass: supClass { private func calcSomething(font: UIFont = UIFont.systemF 我在超类中定义了一个变量并尝试在子类中引用它,但是在实例成员上获取错误不能在类型上使用
class supClass: UIVIEw {    let defaultFontSize: CGfloat = 12.0}class subClass: supClass {    private func calcSomething(Font: UIFont = UIFont.systemFontOfSize(defaultFontSize)) {        //... Do something    }}

它出什么问题了?非常感谢

在类范围上计算方法参数的默认值,
不是实例范围,如下例所示:
class MyClass {    static var foo = "static foo"    var foo = "instance foo"    func calcSomething(x: String = foo) {        print("x =",x)    }} let obj = MyClass()obj.calcSomething() // x = static foo

如果没有静态var foo,它就不会编译.

用于您的情况,这意味着您必须使用所使用的属性
作为默认值static:

class supClass: UIVIEw {    static let defaultFontSize: CGfloat = 12.0 // <--- add `static` here}class subClass: supClass {    private func calcSomething(Font: UIFont = UIFont.systemFontOfSize(defaultFontSize)) {        //... Do something    }}

(请注意,无论是否在中定义了属性,都与此问题无关同一个班级或超级班.)

总结

以上是内存溢出为你收集整理的Swift Instance成员不能用于类型全部内容,希望文章能够帮你解决Swift Instance成员不能用于类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1035734.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存