In fact, polymorphism works as expected. If this did not work, nothing would be printed (0.0000 is printed in your example). The fact is that your instance actually responds to the testFloat:10.0f , since the compiler cannot statically see the method declaration (since the UIView class UIView not declare such a method), it assumes that your method takes ... as an argument and returns id .
When CGFloat is passed to a method that expects a variable number of arguments ( ... ), it advances to double . Thus, the receive method is passed with the double argument and considers it a float , and it does not print correctly.
You can check this behavior by changing the NSLog line to:
NSLog(@"%f", *(double*)&x);
When the compiler sends the message FloatView* , not UIView* , it can find the exact signature of the method. He can see that he really expects CGFloat and does not support the double argument. As a result, it works correctly.
Also, if UIView* contains the method declaration that CGFloat took, the compiler would CGFloat this method accordingly. To summarize, this is not a polymorphism problem; this is a missing signature issue.
source share