xcode 使用元类型值构造类类型“ClassName”的对象必须使用“必需的”初始值设定项 XCode8 Swift 3 更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39919690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:24:55  来源:igfitidea点击:

Constructing an object of class type 'ClassName' with a metatype value must use a 'required' initializer XCode8 Swift 3 changes

iosxcodeswift3

提问by TomV

I'm struggling with this particular error as a result of XCode 8 swift 3 changes, and can't find anywhere a detailed explanation as to why this is happening.

由于 XCode 8 swift 3 的变化,我正在努力解决这个特定的错误,并且找不到任何关于为什么会发生这种情况的详细解释。

Error:

错误:

Constructing an object of class type 'PermissionScope' with a metatype value must use a 'required' initializer

使用元类型值构造类类型“PermissionScope”的对象必须使用“必需的”初始值设定项

In Method:

在方法中:

public convenience init() {
    type(of: self).init(backgroundTapCancels: true)
}

Any help would be appreciated.

任何帮助,将不胜感激。

回答by yy_xuelangwang

i think it means you should add the world 'required'

我认为这意味着您应该添加“必需”世界

class PermissionScope {

   required init(backgroundTapCancels : Bool){

   }
}

回答by Pretty Prasad

TomV, I guess why below thing worked, is that from conv init you must call designated initializer. And also you cannnot call conv init directly from some other class as initiializer. So you might have been called this conv init from same class from some other init method due to which self is available to call further init.

TomV,我猜为什么下面的事情有效,是从 conv init 你必须调用指定的初始值设定项。而且你不能直接从其他类调用 conv init 作为初始化程序。因此,您可能已经从其他一些 init 方法的同一类中调用了这个 conv init,因为它的 self 可用于进一步调用 init。

回答by TomV

This compiled:

这编译:

public convenience init() {
    self.init(backgroundTapCancels: true)
}

回答by Renato Matos

self is different in instance method and class method, you can see it through the auto completion of Xcode. In instance method, self's type is SomeClass while in class method, it's type is SomeClass.Type.

self 的实例方法和类方法不同,可以通过Xcode的自动补全看到。在实例方法中,self 的类型是 SomeClass,而在类方法中,它的类型是 SomeClass.Type。

The compiler complains constructing an object of class type ‘SomeClass' with a metatype value must use a ‘required' initializer

编译器抱怨使用元类型值构造类类型“SomeClass”的对象必须使用“必需”初始化程序

How to understand this? If I write a subclass like this.

这个怎么理解?如果我写一个这样的子类。

See the full explanation here: http://blog.fujianjin6471.com/2015/08/31/why-does-constructing-an-object-with-a-metatype-need-a-required-initializer.html

请参阅此处的完整解释:http: //blog.fujianjin6471.com/2015/08/31/why-does-constructing-an-object-with-a-metatype-need-a-required-initializer.html