Swift: 'var' declaration without getter/setter method not allowed here

前端 未结 2 1973
-上瘾入骨i
-上瘾入骨i 2021-02-05 15:01

I\'ve tried to declare IBOutlet property on extension of class. But it give error as

\'var\' declaration without getter/setter method not al

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 15:36

    From The Swift Programming Language:

    Extensions in Swift can:

    • Add computed properties and computed static properties
    • Define instance methods and type methods
    • Provide new initializers
    • Define subscripts
    • Define and use new nested types

    Which means you can't add IBOutlets and other stored properties. If you really want to cheat, you can create global vars or a bookkeeping object which would allow you to query these vars or the object in order to add those properties (and have them be computed properties).

    But it seems like it would go against the best practices. I would only do it if there's absolutely no other way.

提交回复
热议问题