Unable to call initializer for type without arguments - Swift

I am moving from objective-c to fast, and all I am trying to do is just instantiate the class so that I can access the property in the specified class.

var myClassInstance = MyClass()

print("length is \(myClassInstance.variableOne)")

something in this regard, but I get an error Cannot invoke initializer for type 'MyClass' with no arguments

+4
source share
1 answer

You can create a class as shown below:

class MyClass{
var variableOne:CGPoint

    init(variableOne: CGPoint) {
        self.variableOne = variableOne
    }

    convenience init () {
        self.init(variableOne: CGPoint(x: 0, y: 0))
    }
}


var myClassInstance = MyClass()
print("Default length is \(myClassInstance.variableOne)")

var myClassInstance2 = MyClass(variableOne: CGPoint(x: 10, y: 10))
print("length is \(myClassInstance2.variableOne)")
+6
source

Source: https://habr.com/ru/post/1651240/


All Articles