在 Swift 的类扩展函数中使用“self" [英] Using 'self' in class extension functions in Swift

查看:53
本文介绍了在 Swift 的类扩展函数中使用“self"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从 Nib 中提取 UIView 子类的实例.

I'm looking to be able to pull out an instance of a UIView subclass from a Nib.

我希望能够调用 MyCustomView.instantiateFromNib() 并拥有一个 MyCustomView 实例.我几乎准备好通过桥接头移植我的工作 Objective-C 代码,但我想我会先尝试惯用的方法.那是两个小时前.

I'd like to be able to call MyCustomView.instantiateFromNib() and have an instance of MyCustomView. I'm almost ready to just port the working Objective-C code I have via the bridging header, but figured I'd try the idiomatic approach first. That was two hours ago.

extension UIView {
    class func instantiateFromNib() -> Self? {

        let topLevelObjects = NSBundle.mainBundle().loadNibNamed("CustomViews", owner: nil, options: nil)

        for topLevelObject in topLevelObjects {
            if (topLevelObject is self) {
                return topLevelObject
            }
        }

        return nil
    }
}

现在 if (topLevelObject is self) { 是错误的,因为'is' 之后的预期类型".我在那之后的尝试显示了很多我对 Swift 类型系统不了解的地方.

Now if (topLevelObject is self) { is wrong because "Expected type after 'is'". What I've tried after that shows a lot about what I don't understand about the Swift type system.

  • if (topLevelObject is Self) {
  • if (topLevelObject is self.dynamicType) {
  • if (topLevelObject is self.self) {
  • 一百万个其他甚至没有错的变体.

感谢任何见解.

推荐答案

使用来自 如何在 NSManagedObject Swift 扩展中创建托管对象子类的实例?你可以定义一个通用的辅助方法,它从调用上下文推断 self 的类型:

Using the approach from How can I create instances of managed object subclasses in a NSManagedObject Swift extension? you can define a generic helper method which infers the type of self from the calling context:

extension UIView {

    class func instantiateFromNib() -> Self? {
        return instantiateFromNibHelper()
    }

    private class func instantiateFromNibHelper<T>() -> T? {
        let topLevelObjects = NSBundle.mainBundle().loadNibNamed("CustomViews", owner: nil, options: nil)

        for topLevelObject in topLevelObjects {
            if let object = topLevelObject as? T {
                return object
            }
        }
        return nil
    }
}

这在我的快速测试中按预期编译和工作.如果MyCustomView 是你的 UIView 子类

This compiles and works as expected in my quick test. If MyCustomView is your UIView subclass then

if let customView = MyCustomView.instantiateFromNib() {
    // `customView` is a `MyCustomView`
    // ...
} else {
    // Not found in Nib file
}

给你一个MyCustomView的实例,类型是自动推断.

gives you an instance of MyCustomView, and the type is inferred automatically.

Swift 3 更新:

extension UIView {

    class func instantiateFromNib() -> Self? {
        return instantiateFromNibHelper()
    }

    private class func instantiateFromNibHelper<T>() -> T? {
        if let topLevelObjects = Bundle.main.loadNibNamed("CustomViews", owner: nil, options: nil) {
            for topLevelObject in topLevelObjects {
                if let object = topLevelObject as? T {
                    return object
                }
            }
        }
        return nil
    }
}

这篇关于在 Swift 的类扩展函数中使用“self"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆