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
.
The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason why we use self is that Python does not use the '@' syntax to refer to instance attributes.
The self keyword is used to represent an instance (object) of the given class. In this case, the two Cat objects cat1 and cat2 have their own name and age attributes.
The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.
self ) allows the metaclass to inspect the class attributes without having to first instantiate it.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With