0

In my app, I need to store a single PKStroke and then be able to find it in a canvasView.drawing.strokes array and modify it.

If I'm correct, I need PKStroke to conform to Equatable protocol.

I've found two ways of doing it:

//1.
extension PKStroke: Equatable {
    public static func ==(lhs: PKStroke, rhs: PKStroke) -> Bool {
        //We compare date and the number of points
        return lhs.path.creationDate == rhs.path.creationDate &&
        lhs.path.count == rhs.path.count
    }
}
//OR
//2.
extension PKStroke: Equatable {
    public static func ==(lhs: PKStroke, rhs: PKStroke) -> Bool {
        //We get strokes as references and compare them for identity
        return (lhs as PKStrokeReference) === (rhs as PKStrokeReference)
    }
}

Maybe both ways are wrong. Could you point me in the right direction?

Stan
  • 77
  • 1
  • 7

2 Answers2

0

PKStroke is a struct, so I would compare them by the properties rather than references:

public static func ==(lhs: PKStroke, rhs: PKStroke) -> Bool {
    if lhs.ink.inkType != rhs.ink.inkType { return false}
    if lhs.path.count != rhs.path.count { return false}
    if lhs.renderBounds != rhs.renderBounds { return false}
    if lhs.transform != rhs.transform { return false}

    // Compare more properties here if you want


    return true
 }
Brew Master
  • 457
  • 3
  • 13
-1

This works for me:

extension PKStroke: Equatable {
   public static func ==(lhs: PKStroke, rhs: PKStroke) -> Bool {
       return (lhs as PKStrokeReference) === (rhs as PKStrokeReference)
   }
}