简体   繁体   English

swift 3 中的单例计算属性

[英]Singleton computed property in swift 3

I am trying to achieve a singleton UIView instance in my code, so whenever I create an instance of 'MyView' the nib file will load for only once and then reuse it again:我试图在我的代码中实现一个单例 UIView 实例,所以每当我创建一个 'MyView' 实例时,nib 文件只会加载一次,然后再次重用它:

class MyView : UIView {

    @IBOutlet weak var someLabel: UILabel!
    
    static var customeView : UIView = {

        let view = Bundle.main.loadNibNamed(String(describing: MyView.self), owner: self, options: nil)?[0] as! UIView
        return view
    }()
    
    convenience init() {
        
        self.init(frame: CGRect(x: 0, y: 0, width: 576, height: 30))
        
        let viewForOnce = MyView.customeView
        viewForOnce.frame = bounds
    }
} 

The problem is that whenever I call the MyView.customeView it get crashed saying that 'this class is not key value coding-compliant', I think this is happening because of the 'owner: self' inside the computed property.问题是,每当我调用MyView.customeView它都会崩溃,说“这个类不符合键值编码”,我认为这是因为计算属性中的“所有者:自我”。

Any help will be appreciated.任何帮助将不胜感激。

在此处输入图片说明

The issue is most probably with your ReceiptView.xib .问题很可能与您的ReceiptView.xib You should review it first.你应该先回顾一下。 Look for an IBOutlet , which is there in a xib, but not in your ReceiptView class and delete it.查找IBOutlet ,它存在于 xib 中,但不在您的 ReceiptView 类中,然后将其删除。

Edit:编辑:

I meant with the singleton is loading the nib only once whenever I create and instance of that class.我的意思是,每当我创建该类的实例时,单例只会加载一次笔尖。

  1. No, this is not going to work like that.不,这不会那样工作。 Every time init is called, your computed property is also called and a new instance is created from xib every time.每次调用 init 时,您的计算属性也会被调用,并且每次都会从 xib 创建一个新实例。

  2. As Carien van Zyl already mentioned, you are using self in a class var which corresponds to MyView class itself (or it's subclass if its called for a subclass), not an instance .正如Carien van Zyl已经提到的,您在class var 中使用self ,它对应于MyView类本身(或者它是子类,如果它被称为子类),而不是 instance Try passing nil as owner instead.尝试将nil作为owner传递。

  3. The whole technique is looking wrong to me.整个技术在我看来是错误的。 You should not use singleton pattern with UIView subclasses.你不应该在UIView子类中使用单例模式。 There is nothing wrong in calling loadNibNamed multiple times and create exactly the same instances.多次调用loadNibNamed并创建完全相同的实例并没有错。 If you want to use the same instance multiple times in a view hierarchy, it's not possible since every view can have only one superview.如果你想在一个视图层次结构中多次使用同一个实例,这是不可能的,因为每个视图只能有一个超级视图。 In this case you should follow MVC pattern: create multiple MyView instances -> update model whenever you change something in a view and want those changes to be reflected elsewhere -> update another view using updated model.在这种情况下,您应该遵循 MVC 模式:创建多个MyView实例 -> 每当您更改视图中的某些内容并希望这些更改反映在其他地方时更新模型 -> 使用更新的模型更新另一个视图。

customeView is a type property. customeView是一个类型属性。 Therefor, self inside of it, will reference MyView.self which is a class type.因此,其中的 self 将引用MyView.self ,它是一个类类型。 The class type do not hold the instance variables, which includes someLabel .类类型不包含实例变量,其中包括someLabel

See Apple's documentation on Types请参阅Apple 关于类型的文档

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

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