user2924482
user2924482

Reputation: 9140

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:

enter image description here

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.

Upvotes: 1

Views: 87

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

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

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. 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.

Upvotes: 1

Connor Neville
Connor Neville

Reputation: 7361

Change the name of your 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.

Upvotes: 1

Related Questions