Search code examples
ios13ipadosapple-pencilpencilkit

Why does PKDrawing() != PKDrawing()? (PencilKit)


According to the docs, PKDrawing conforms to Equatable. But if you compare 2 blank drawings with ==, it returns false. I submitted a bug via the feedback app, but I am posting here hoping I missed something or others will also submit a bug report so this can get fixed. I need to check if a PKCanvasView has any content, and being that PKDrawing is Opaque we can't query for strokes or other data. Given the limited api, it seems that the best way to check would be something like this:

extension PKCanvasView {
    func isEmpty() -> Bool {
      return self.drawing == PKDrawing()
    }
}

This will return false though regardless of the canvasView.drawing. Even, PKDrawing() == PKDrawing() returns false.


Solution

  • In this case, you can check bounds of drawing object. And iOS 14 has provided strokes that this drawing contains.

    extension PKDrawing {
        
        func isEmpty() -> Bool {
    
            if #available(iOS 14.0, *) {
                return strokes.isEmpty
            } else {
                return bounds.isEmpty
            }
        }
    }