Question

Sending "messages" in Objective-C is a principal runtime function, which gets call when we compile our source code (though objc_msgSend).

If I understand correctly "message sending" is for instance variables. Since a properties without a weak or strong pointer are not initialized in the Heap, calling a setter's property won't "send a message", it will just call the "function" created automatically by the @property i.e. -(void)setNumber:(int)number , which happens in the Stack right?

Was it helpful?

Solution

Since a properties without a weak or strong pointer are not initialized in the Heap, calling a setter's property won't "send a message"

It will send a message. The setNumber: method in your example is part of the object that owns the int - that is the object to which the message is sent, not the int, which cannot be a target of a message at all.

@interface Demo : NSObject
@property (nonatomic, readwrite) number;
@end

Demo *demo = [[Demo alloc] init]; // Creates the object
// The following two lines are identical - they both send setNumber to demo
demo.number = 123;
[demo setNumber:123];

It is also incorrect to say that primitives are not initialized in the heap: being part of their "host" objects, they are always allocated from the dynamic memory. The fact that they do not point to other heap memory in the same way that id properties do does not change the place where the properties themselves are allocated.

OTHER TIPS

The statement: "message sending is for instance variables" is incorrect. Message sending is done between objects instances. Essentially, "sending a message" is performing a method on a specific object with the given arguments. You can view a method as a specific "function" of an object. Since properties are simply getter and setter methods, accessing a property will "send a message" because it's calling the method on the object that sets/gets the property value.

Also, "Since a properties without a weak or strong pointer are not initialized in the Heap" is incorrect. Properties are simply a combination of a getter (- (NSString *) name;) and setter (- (void) setName:(NSString *)name;). The property itself may or may not be backed by a instance variable, but the definition of a "property" has nothing to do with the "heap" or "stack" directly.

Instance variables (all variables) can be either on the heap or the stack, depending on what that variable is. If it's a primitive (int, double, etc), it exists as a value on the stack. If the variable is an object (type ID or *) the object exists on the heap with a pointer to that object on the stack. The exception to this are c-blocks, which are (as far as I know) the only objects initialized on the stack, but that's a different conversation. The easiest way to know whether a variable is on the heap (is an object) is whether it has a type of ID or you see an asterisk * as part of the type.

You can view a property as an "accessor". They're designed to get or set values related to an object, but the object itself determines how the "getting" and "setting" are done. You can make no assumptions about how the object does this. It may keep a backing instance variable of the value, but it can just as easily also keep the value as a composite of other values. It's entirely up to the object how to store and retrieve the value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top