Frage

I am currently studying the sample code provided by Apple for Sketch and I stumbled upon some syntax that I haven't seen before. It's in SKTGraphicView.m in the function moveSelectedGraphicsWithEvent:

NSRect selBounds = [[SKTGraphic self] boundsOfGraphics:selGraphics];

I have never seen the [SomeClass self] syntax before. In this case self is a subclass of NSView and boundsOfGraphics: is a class method for SKTGraphic which is a subclass of NSObject.

War es hilfreich?

Lösung

The self method is defined in the NSObject protocol, so every object be it an instance of a class or a class object (of type Class) supports the method. It simply returns the object it is called on, i.e. something like:

- (id) self { return self; }

So self on an instance returns the instance, and on a class object returns the class object.

The following therefore holds: [x self] == x is YES for all instance and class objects x

And your line is equivalent to:

NSRect selBounds = [SKTGraphic boundsOfGraphics:selGraphics];

So that is what it does. As to why Apple wrote it this way, that's a different question...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top