Search code examples
objective-ciosstructureparameter-passingcgrect

What is "CG_INLINE CGRect" definition in Objective C called?


What is the following structure called in Objective C,

CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = width; rect.size.height = height;
  return rect;
}

I want to create my own definition for making an object that would hold multiple parameters that I pass. Currently I am using a static method within a class that would take multiple parameters and return an object. But this syntax of CGRectMake is a perfect solution i could use. What is the structure actually called in Objective C syntax?


Solution

  • CGRectMake() is a function. Functions are a C language feature, not unique to Objective-C. Therefore, they don't have a special name in Objective-C. They're called C functions.

    If you're asking about this seemingly cryptic syntax:

    CG_INLINE CGRect
    CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
    
    1. CG_INLINE is a special token that does nothing more than declare a function as inline. You can find a description of CG_INLINE in this question:

      What does CG_INLINE do?

    2. CGRect is the return type. The line break that follows is mere whitespace; it's a somewhat common code-formatting convention in C to break a line after the return type in a function definition.

    3. CGRectMake is the function name.

    4. The rest that follows is its argument list and function body.

    If you want to create a function, you can place its definition in your .m file, with a corresponding forward declaration in the .h file.