简体   繁体   English

Swift:创建iOS框架并导入框架错误:变量在其初始值内使用

[英]Swift: Creating iOS framework and importing the framework error : Variable used within its own initial value

I'm trying to build a iOS framework but at the moment I'm trying to import the framework I'm getting this error: 我正在尝试构建一个iOS框架,但此刻我正在尝试导入该框架,但出现此错误:

在此处输入图片说明

This is implementation of my framework: 这是我的框架的实现:

public protocol myframeWorkDelegate{
    func doSomething(value:Int)
}
public class myframeWork{
    public var delegate:myframeWorkDelegate? = nil

    public func doingSomething(do:String){

    }
}

Any of you knows why I'm getting this error? 你们谁都知道为什么我会收到此错误?

I'll really appreciate your help. 非常感谢您的帮助。

Change the name of your var . 更改您的var的名称。 The error is because the name of the class is the same as the name of the variable. 该错误是因为类的名称与变量的名称相同。 Additionally, this is why you should always capitalize class names ie class MyFramework versus var myFramework . 另外,这就是为什么您应该始终大写类名的原因,即class MyFrameworkvar myFramework

This happens because you named your variable the name of your class. 发生这种情况是因为您将变量命名为类的名称。 Swift compiler starts using the name of your variable right away, and decides that you are trying to read the value inside its own initializer, as in Swift编译器立即开始使用变量的名称,并决定您正尝试在其自己的初始化程序中读取该值,如下所示:

var someVariable : String = someVariable

Of course, you are not doing this, so Swift compiler could distinguish between the two uses of myframeWork identifier in the declaration, at least theoretically. 当然,您没有这样做,因此Swift编译器至少在理论上可以区分声明中myframeWork标识符的两种用法。 However, they decided it is not worth the trouble. 但是,他们认为不值得这样做。

Renaming the variable or the class will fix this problem. 重命名变量或类将解决此问题。 You may also need to provide a public initializer for your class in order for the modified code to compile. 您可能还需要为您的类提供一个公共初始化程序,以便编译修改后的代码。

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

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