简体   繁体   English

在“self.init”调用之前使用“self”错误或在不同模块中的 init 上分配给“self”

[英]Error of 'self' used before 'self.init' call or assignment to 'self' on init in a different module

I have checked questions sharing the same topic but none address this weird behaviour I am experiencing:我检查了共享相同主题的问题,但没有一个解决我遇到的这种奇怪行为:

Say I have a simple old school struct :假设我有一个简单的老派struct

struct Person {
   var name: String
   var age: Int
}

And I want to overload the init in an extension like this:我想在这样的extension中重载init

extension Person {
   init(name: String) {
       self.name = name
       self.age = 26
   }
}

As you expected, this code would run just fine.正如您所料,这段代码运行得很好。

However, If I move the Person struct to a different module (aka different framework) and expose it to my module like this:但是,如果我将Person struct移动到不同的module (也称为不同的框架)并将其公开给我的模块,如下所示:

public struct Person {
   public var name: String
   public var age: Int
}

If I now overload the init in an extension locally in my module the compiler produces the following errors:如果我现在在module的本地extension中重载init ,编译器会产生以下错误:

'self' used before 'self.init' call or assignment to 'self'

'self.init' isn't called on all paths before returning from initializer

The only way I found to avoid this problem is calling the original init inside the overloaded one like this:我发现避免这个问题的唯一方法是在重载的初始化中调用原始init ,如下所示:

 extension Person {
    init(name: String) {
       self.init(name: name, age: 24)
    }
 }

I personally find this behaviour to be pretty strange.我个人觉得这种行为很奇怪。

Am I missing something?我错过了什么吗?

Actually this example works for me with only a warning saying Initializer for struct 'Person' must use "self.init(...)" or "self =..." because it is not in module .实际上这个例子对我有用,只有一个警告说Initializer for struct 'Person' must use "self.init(...)" or "self =..." because it is not in module As far as I know it was by design that struct initialisers were enforced to be defined within the scope of the struct definition module since Swift 4.2.据我所知,自 Swift 4.2 以来,结构初始化程序被强制在结构定义模块的 scope 中定义是设计使然。 Check the motivation section in ' https://github.com/apple/swift-evolution/blob/master/proposals/0189-restrict-cross-module-struct-initializers.md '.检查“ https://github.com/apple/swift-evolution/blob/master/proposals/0189-restrict-cross-module-struct-initializers.md ”中的动机部分。

暂无
暂无

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

相关问题 如何纠正在调用“ self.init”或分配给“ self”之前使用的“ self”错误? - How to rectify the error of “self' used before 'self.init' call or assignment to 'self'”? Swift:方便初始化程序 - 在self.init调用之前自我使用 - Swift: Convenience initializers - Self used before self.init call 为什么在init中使用DispatchQueue时会在self.init调用之前使用错误“ self”? - Why error ''self' used before self.init call' when using DispatchQueue in init? 在自定义类上使用NSCoding时在self.init调用错误之前使用的'self' - 'self' used before self.init call error while using NSCoding on a custom class 在自定义UIGestureRecognizer类中调用self.init之前使用的self - Self used before self.init called in custom UIGestureRecognizer class 错误:在self.init初始化self之前,在属性访问“ content”中使用“ self” - Error :Use of 'self' in property access 'content' before self.init initializes self 在调用self.init之前自己使用的iOS Swift便利初始化程序 - iOS Swift convenience initializer self used before self.init called 当我添加了 self.init(url: url) 时,为什么要求调用 self.init? - Why is it asking for self.init to be called, when I have added self.init(url: url)? RealmSwift初始值设定项:在super.init调用之前自行使用 - RealmSwift initializer: self used before super.init call 在super.init调用之前使用“ self”,并且在super.init调用中未初始化属性 - 'self' used before super.init call and property not initialized at super.init call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM