ios - Should CIImage be Equatable? -
so, apple’s documentation says ciimage conforms equatable. take mean following unit test pass. however, doesn’t. i’m interested in why.
func test_ciimageequalityshouldwork() { let bundle = nsbundle(forclass: prototypetests.self) guard let path = bundle.pathforresource("testimage", oftype: "png") else { return } guard let image = uiimage(contentsoffile: path) else { return } let thingy1 = ciimage(image: image) let thingy2 = ciimage(image: image) xctassert(thingy1 == thingy2) } the image exists, guard statements both pass, assert fails, aren’t equal.
out of interest, i’ve tried creating uiimage twice , comparing too. fails.
all nsobject subclasses conform equatable, , == function calls isequal: method on objects. isequal: method of nsobject compares object pointers, i.e. o1 == o2 holds if o1 , o2 refer same object instance.
see example interacting objective-c apis:
swift provides default implementations of == , === operators , adopts equatable protocol objects derive nsobject class. default implementation of == operator invokes isequal: method, , default implementation of === operator checks pointer equality. should not override equality or identity operators types imported objective-c.
the base implementation of isequal: provided nsobject class equivalent identity check pointer equality.
many nsobject subclasses override isequal: method (e.g. nsstring, nsarray, nsdate, ...) not ciimage:
let thingy1 = ciimage(image: image) let thingy2 = ciimage(image: image) creates 2 different ciimage instances , these compare "not equal".
Comments
Post a Comment