Difference between rect and self

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Difference between rect and self



For example, the draw() API:


draw()


override func draw(_ rect: CGRect) {



It has a rect parameter. I've always assumed it's the view, but now I'm not sure this is the case. I've also assumed that self, when used in a subclass of UIView, is also the view. By printing out the same property of each on the console, I can see that self and rect are not the same thing.


rect


self


UIView


self


rect



So, what's the difference?




2 Answers
2



According to Docs



rect



The portion of the view’s bounds that needs to be updated. The first time your view is drawn, this rectangle is typically the entire visible bounds of your view. However, during subsequent drawing operations, the rectangle may specify only part of your view.



self : is the view itself





I still don't really get the difference. Doesn't the whole view "need to be updated"?
– Knarf Oag
yesterday





Not necessarily, no. That's the point. "During subsequent drawing operations, the rectangle may specify only part of your view."
– matt
yesterday







rect is where the view laids in terms of location , self is the object of the current view where you can access the properties of the class and process them
– Sh_Khan
yesterday





@matt it's the documentation
– Sh_Khan
yesterday





if say the view's frame is (100,100,200,300) , then rect is (0,0,200,300)
– Sh_Khan
yesterday



In your example, rect is an instance of the CGRect struct, which is a Core Graphics struct simply defining a rectangle on the screen, or part of a UIView. It can be initiated in a number of ways (using corner coordinates, dimensions etc). You can read more in the Apple documentation.


rect


CGRect


UIView



The term self is not related to rect. As is typical of many object-orientated programming languages, self refers to the instance of the class your code is in. In iOS development, this is commonly within a UIViewController subclass, where self always refers to that instance of the view controller.


self


rect


self


UIViewController


self





Just to note that CGRect is a structure not a class...
– Alladinian
yesterday


CGRect


structure





@Alladinian Well spotted, thanks. I will update.
– Chris
yesterday






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

415 Unsupported Media Type while sending json file over REST Template

PHP parse/syntax errors; and how to solve them?