简体   繁体   English

有什么解决方法可以快速将存储的属性放入扩展中?

[英]is there any workaround to put stored properties into extensions in swift?

I'm creating my project all in code and it was a bit confusing and difficult to read because there were several lines just to set up UIElements in my code... 我正在用代码创建我的项目,这有点令人困惑且难以阅读,因为在我的代码中有几行只是用来设置UIElements ...

so I tried to insert this bulky part into an extension and got an error saying that stored properties can't go into extensions, after that my solution was as follows... 所以我试图将这个笨重的部分插入扩展中,并收到一条错误消息,指出存储的属性无法进入扩展,之后我的解决方案如下...

weak var centraliseButton: UIButton!
weak var infoButton: UIButton!
weak var seeFireButton: UIButton!
weak var locatManager: CLLocationManager!
weak var mapkitView: MKMapView!
weak var containerViewForInfoButton: UIView!
weak var containerViewForSeeFireButton: UIView!

and then I set up evertyhing in an function in a extension file and call this function inside viewDidLoad() , but again I got tons of runtime errors and nils because I access self inside some of these properties and they were lazy var before, so my last solution was the following: 然后我在扩展文件中的函数中设置了evertyhing,并在viewDidLoad()调用此函数,但是再次出现大量的运行时错误和nil,因为我在其中一些属性中访问self,并且之前它们是lazy var ,所以我最后的解决方案是:

lazy var centraliseButton: UIButton = UIButton()
lazy var infoButton: UIButton = UIButton()
lazy var seeFireButton: UIButton = UIButton()
lazy var locatManager: CLLocationManager = CLLocationManager()
lazy var mapkitView: MKMapView = MKMapView()
lazy var containerViewForInfoButton: UIView = UIView()
lazy var containerViewForSeeFireButton: UIView = UIView()

and set everything inside the function in a extension, it worked I got no errors but it looks like a bad solution to me, I had to initialize all of them with a 'dummy' value because lazy var require being initialized right away so... 并在扩展中将函数中的所有内容都设置了,它可以正常工作,但没有错误,但对我来说似乎是一个不好的解决方案,我必须使用“虚拟”值初始化所有参数,因为lazy var要求立即进行初始化。 。

Is it a bad practise? 这是坏习惯吗? Is there any better way to accomplish that? 有没有更好的方法可以做到这一点?

thank you in advance for the answers! 预先感谢您的回答!

I assume you have a really good reason for not just storing these properties in the class itself. 我认为您有充分的理由不只是将这些属性存储在类本身中。

So the way to work around it is to use objc_getAssociatedObject and objc_setAssociatedObject. 因此,解决该问题的方法是使用objc_getAssociatedObject和objc_setAssociatedObject。

For example, in your extension you could put: 例如,在扩展程序中,您可以输入:

struct AssociatedKeys 
{
    static var infoButton: UInt8 = 0
}

private(set) var infoButton: UIButton? 
{
    get 
    {
        guard let value = objc_getAssociatedObject(self, &AssociatedKeys.infoButton) as? UIButton else 
        {
            return nil
        }
        return value
    }
    set(newValue) 
    {
        objc_setAssociatedObject(self, &AssociatedKeys.infoButton, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM