Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the [SomeClass self] syntax do?

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.

like image 646
Sebastian Avatar asked Mar 21 '14 17:03

Sebastian


People also ask

What does the self argument do in Python?

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.

What is the use of the keyword self?

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.

What is __ init __( self in Python?

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.

What is the use of self in Django?

self ) allows the metaclass to inspect the class attributes without having to first instantiate it.


1 Answers

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...

like image 125
CRD Avatar answered Oct 16 '22 23:10

CRD