I don’t understand how use the functions of MyUtils:
import Foundation import CoreGraphics func + (left: CGPoint, right: CGPoint) -> CGPoint { return CGPoint(x: left.x + right.x, y: left.y + right.y) }
then a example to use:
let testPoint1 = CGPoint(x: 100, y: 100) let testPoint2 = CGPoint(x: 50, y: 50) let testPoint3 = testPoint1 + testPoint2
but what i spect to use is:
let testPoint3 = +(testPoint1,testPoint2)
?
The key here is func +
. The Swift compiler distinguishes between different unicode characters when it works out what you’ve typed; some characters (such as letters, e.g. in ‘add’) will be recognised as part of an identifier, which could be used in a function call (e.g. ‘add(testPoint1, testPoint2)
’. Other characters (such as +
) will be recognised as part of an operator.
An operator is still a function, though, so its implementation is declared in the same way, but it’s used as you’d expect a mathematical operator to be used, between the two arguments.
1 Like
OK,thanks for your answer.