1

I know that calling a method's instance like [object theMethod] is a message to that instance asking to perform an action (a method named theMethod).

Apple's documentation doesn't help when it comes to CGRectMake.

For example:

When sending a message to an instance the compiler converts a message expression:

[receiver message]

into a call to a messaging function, objc_msgSend. This function takes the receiver and the name of the method mentioned in the message—that is, the method selector—as its two principal parameters:

objc_msgSend(receiver, selector)

Any arguments passed in the message are also handed to objc_msgSend:

objc_msgSend(receiver, selector, arg1, arg2, ...)

But what about CGRectMake? Where is that function is located? What's the main differences (procedure) when calling the functions?

Update: What about memory? For example blocks are created in the stack. What about CGRectMake?

BlackM
  • 3,927
  • 8
  • 39
  • 69
  • Please update your question with some specific examples of code you need help with. – rmaddy Nov 14 '16 at 16:56
  • `[object theMethod]` is Objective-C semantics, whereas functions like `CGRectMake()` is an inline C function call. See http://stackoverflow.com/questions/6207440/what-does-cg-inline-do. – fullofsquirrels Nov 14 '16 at 17:01
  • I think that is not related to only Objective-C. It's more about OOP vs non-OOP. Like C vs C++, struct vs objects, (objects/class) methods vs functions etc. I think that even if it's an interesting question, it's quite broad as it would require a long answer, pros/cons, etc. – Larme Nov 14 '16 at 17:03
  • @Larme Maybe it's a broad topic but is there any other stack.* for this kind of questions? – BlackM Nov 14 '16 at 17:06
  • It's just my opinion, here is are some hints: http://stackoverflow.com/questions/155609/difference-between-a-method-and-a-function I think that if you search for "method vs function", on SO, you should be able to find interesting answer, but I think they don't cover every part, since it's about two different concepts, at least, from what I understood of your question. – Larme Nov 14 '16 at 17:09
  • I know the difference between methods and functions but Objective-C is a special case since is a "wrapper" for C. Anyway thank you for your reply! – BlackM Nov 14 '16 at 17:12

1 Answers1

6

The difference between methods and “plain functions” is a sad consequence of Objective-C being a relatively simple wrapper around C. Some types that were considered too lightweight to implement as classes were implemented using structs or other more primitive types, and are accompanied by plain C functions or macros to work with them. You can Command-click a function name in Xcode to see the declarations and definitions:

// CoreGraphics / CGGeometry.h
struct CGRect {
    CGPoint origin;
    CGSize size;
};
typedef struct CGRect CGRect;

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;
}

As for the calling differences, you mostly already know them. Messages are translated into objc_msgSend, whereas functions such as CGRectMake are plain C functions or macros with the usual semantics.

zoul
  • 102,279
  • 44
  • 260
  • 354