In Objective-C there is a type named SEL. It’s defined like this:
id (*SEL)(id self, SEL _cmd,...);
but if we make a method whose return type is double, the id type is not good to use.
How can the Objective-C runtime work with this problem?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
SELis defined astypedef struct objc_selector *SEL;, so you are wrong.SELis a type used to store a selector, which basically is the “name” of a method, along with some other information.Anyway,
In the case of a
doublereturn type, Objective-C will simply return a double. It doesn’t care about howSELandidare declared. (Just likeprintfbut the other way around: return value instead of function arguments.)The implementation of the
+[A a]method in this sample program:Gives this assembly output (which is a function with a generated name you don’t know: you can’t just call it by its name*):
It just returns a
double, even if adoubleneeds more bytes thanid. It isn’t translated to C in between, so this is no problem. Note that the returned values of Objective-C methods are not type-safe.The main function calls
_objc_msgSend(which calls the “nameless” function pointed to by theIMP*) and puts the return value of it intob.*the address of that function is stored in the method definition as a variable of type
IMP. AnIMPis definedid (*IMP)(id, SEL, ...).