Self.Type 不能直接转换为 AnyClass 在 swift 中扩展到objective-c 类 [英] Self.Type cannot be directly converted to AnyClass in extension to objective-c class in swift

查看:23
本文介绍了Self.Type 不能直接转换为 AnyClass 在 swift 中扩展到objective-c 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建结构方法来创建具有正确笔尖名称的 UIViewController(以修复 iOS8 默认初始化程序问题).为此,我添加了扩展名:

I'm trying to create fabric method to create UIViewController with correct nib name (to fix iOS8 default initialiser issue). To do it I have added extension:

extension UIViewController {    
    class func create() -> Self {
        if #available(iOS 9.0, *) {
            return self.init()
        } else {
            let clsName = NSStringFromClass(self).componentsSeparatedByString(".").last!
            return self.init(nibName: clsName, bundle: nil)
        }
    }
}

然而编译器发出错误:无法将类型Self.Type"的值转换为预期的参数类型AnyClass"(又名AnyObject.Type") in NSStringFromClass(self)代码>.

However compiler issues error: Cannot convert value of type 'Self.Type' to expected argument type 'AnyClass' (aka 'AnyObject.Type') in NSStringFromClass(self).

要修复它,可以添加另一个扩展方法并将代码重写为:

To fix it another extension method can be added and code rewritten to:

extension UIViewController {
    private class var nibNameForInitializer:String {
        return NSStringFromClass(self).componentsSeparatedByString(".").last!
    }
    class func create_() -> Self {
        if #available(iOS 9.0, *) {
            return self.init()
        } else {
            return self.init(nibName: self.nibNameForInitializer, bundle: nil)
        }
    }
}

但是我想了解第一个变体的问题.

However I want to understand the problem with first variant.

据我所知,返回Self的方法是一种泛型方法.Self 可以在许多上下文中使用(例如类、结构体、协议等),但是 Self.Type 仅与类的 AnyClass 匹配.

As I understand, method returning Self is a kind of generic method. Self can be used in many contexts (e.g. class, struct, protocol, etc), however Self.Type matches AnyClass only for classes.

就我而言,编译器应该知道 Self 指的是 UIViewController 及其所有子类(因为它在 UIViewController 扩展中),所以Self.Type 必须可转换为 AnyClass.

In my case, compiler should know that Self refers to UIViewController and all its subclasses (since it is inside UIViewController extension), so Self.Type must be convertible to AnyClass.

我是否遗漏了什么,或者这是正确的行为,因为编译器不会对 Self 执行任何额外的类型分析?

Do I miss anything, or it is correct behaviour, since compiler doesn't perform any additional type analysis for Self?

推荐答案

这看起来像是一个错误或(不必要的)限制,所以你可能考虑向 Apple 提交错误报告.它只发生对于返回类型为 Self 的类型方法.作为解决方法,您可以编写

This looks like a bug or an (unnecessary) restriction, so you might consider to file a bug report at Apple. It happens only for type methods with return type Self. As a workaround, you can write

let clsName = NSStringFromClass(self as! AnyClass).componentsSeparatedByString(".").last!

它编译并似乎按预期工作.

which compiles and seems to work as expected.

但是有一种更简单的方法可以得到相同的结果:

But there is a simpler way to get the same result:

// Swift 2:
let clsName = String(self)
// Swift 3:
let clsName = String(describing: self)

这篇关于Self.Type 不能直接转换为 AnyClass 在 swift 中扩展到objective-c 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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