Question

I just created a normal MyClass and defined an instance variable

@interface MyMFMessageComposeViewController : NSObject
{
    NSString *body;
    NSArray * recipients; 
}

@property (readwrite, strong) NSString *body;
@property (readwrite, strong) NSArray * recipients; 
@end

then I coreced to MFMessageComposeViewController.

and it worked - surprisingly - for some reason the class variables got transferred correcty.

How does this work? Does the runtime really search for class variable NAMES to find the appropriate class variables?

I thought coercion would just be an overlay of memory.

Many thanks for your insight?

Was it helpful?

Solution

Objective-C is weakly typed (some people even say it's untyped), meaning that that the exact type of an object doesn't matter, as long as it responds to all of the selectors (method names) the program tries to send it.

MyMFMessageComposeViewController and MFMessageComposeViewController both have methods called body, setBody, recipients, and setRecipients because both classes declare and synthesize properties named body and recipients. (Remember that synthesizing properties just creates these accessor methods.) If you access the member variables from an external object, you're using these methods, even if you use dot notation to do it.

In short, you're not accessing the instance variables directly; instead, you're using accessor methods, and because Objective-C doesn't care about the type of an object when it invokes methods, it all works just fine.

All that being said, you should delete this class and replace it with a proper subclass, at which point you (probably) won't have to worry about typecasting.

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