Search code examples
xcodeswiftswift-playground

Swift '(int, int) -> $T4' is not identical to 'Point'


I'm fairly new to swift and get this error. All I want the declaration to do is assign the 4 values to create 2 Point objects for a Line object. So Line(10, 20, 20, 40) should create two points at (10,20) and (20,40). But I get the error '(Int, Int) -> $T4' is not identical to 'Point'

class Point {
var x: Int = 0
var y: Int = 0

init(){
    x = 0
    y = 0
}

init(x: Int, y: Int) {
    self.x = x
    self.y = y
  }
}

class Line: Point {
var pointA: Point
var pointB: Point

init(p1x: Int, p1y: Int, p2x: Int, p2y: Int) {
    self.pointA(p1x, p1y)
    self.pointB(p2x, p2y)
  }
}

var lineA = Line(p1x:10, p1y:20 , p2x:20 , p2y:40)

Solution

  • Your code in Line's init is meaningless. This is not valid Swift:

    self.pointA(p1x, p1y)
    self.pointB(p2x, p2y)
    

    You can't just stick parentheses after the name of a variable like that! Plus you are not writing your init correctly. Here's a possible fix:

    class Line: Point {
        var pointA: Point
        var pointB: Point
    
        init(p1x: Int, p1y: Int, p2x: Int, p2y: Int) {
            self.pointA = Point(x:p1x, y:p1y)
            self.pointB = Point(x:p2x, y:p2y)
            super.init()
        }
    }
    

    But as Antonio has already said, it all depends on what you're trying to do. My code above will cause your code to compile correctly, but I have no idea whether it does what you want, because what you're trying to do is utterly impossible to guess.