-1
let dx = line.frame.midX - other.frame.midX

produces the following exception:

Ambiguous use of operator '-'

Here is the line & other class' object type declaration:

import Foundation
import SpriteKit

class Line: SKShapeNode {
    var plane:Plane!

    init(rect: CGRect, plane: Plane) {
        super.init()
        self.path =  CGPathCreateWithRect(rect, nil)
        self.plane = plane
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

I noticed that when I Jump to definition ( + click) in Xcode the frame object takes me to the frame property in UIView instead of SKNode.

Am I not subclassing correctly? How do I get rid of the Ambiguous use of operator exception error?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
sdc
  • 2,603
  • 1
  • 27
  • 40
  • 1
    What is the class for the `other` reference? – Price Ringo Mar 26 '16 at 20:58
  • @PriceRingo other is also a of type Line class – sdc Mar 26 '16 at 21:09
  • I can not explain the error whether both `line` and `other` are both `Line` objects and even `Line` and a `UIView` objects. The `ambiguous` error message is thrown when the compiler can not resolve the types on either side of the `-` operator. Make sure that both are `CGFloat`. That will be the starting point for figuring this out. – Price Ringo Mar 26 '16 at 21:32
  • @PriceRingo, Yes! That is my problem. The midX property in both the `line.frame` and `other.frame` object can not be resolved. Working backwards, both the frame objects are acting as if they are `UIView` objects, which explains why they do not have a `midX` property. But why do the frame objects think that they are `UIView` objects instead of `SKNode` objects – sdc Mar 26 '16 at 21:39

1 Answers1

3

The problem arises because you are importing neither UIKit nor SpriteKit in the class in which you create these objects.

For instance, if you had, say, GameViewController class:

class GameViewController : UIViewController

Make sure to import either of the aforementioned frameworks:

I.e.,

import UIKit
//or
import SpriteKit

class GameViewController : UIViewController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        let line = Line(rect:....) //specify rect 
        let other = Line(rect:....) //specify rect

        let dx = line.frame.midX - other.frame.midX
    }

}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • 1
    Thanks! That was my issue. I was actually doing these calculations in a Utility / Helper class and I forgot to import `SpriteKit` – sdc Mar 26 '16 at 21:42