Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6768197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:02:48+00:00 2026-05-26T15:02:48+00:00

I have just started learning Objective-C, I am reading Programming in Objective-C 3rd Edition

  • 0

I have just started learning Objective-C, I am reading Programming in Objective-C 3rd Edition by Stephen G. Kochan.

There’s a paragraph explaining the polymorphism mechanism:

At runtime, the Objective-C runtime system will check the actual class of the object stored inside dataValue1(an id object) and select the appropriate method from the correct class to execute. However, in a more general case, the compiler might generate the incorrect code to pass arguments to a method or handle its return value.This would happen if one method took an object as its argument and the other took a floating-point value, for example. Or
if one method returned an object and the other returned an integer, for example. If the inconsistency between two methods is just a different type of object (for example, the Fraction’s add: method takes a Fraction object as its argument and returns one, and the Complex’s add: method takes and returns a Complex object), the compiler will still generate the correct code because memory addresses (that is, pointers) are passed as references to objects anyway.

I don’t quite get it that the first part of the paragraph says that the compiler might generate incorrect code if I declare 2 methods in different classes with the same name and different type of arguments. while the last part of the paragraph says that it is fine to have 2 methods with the same name and different arguments and return types…oh no…

I have the following code and they compile and run fine:

@implementation A
- (int) add:(int)a {
    return 1 + a;
}
@end
@implementation B
- (int) add: (B*) b {
    return 100;
}
@end
id a = [[A alloc] init];
id b = [[B alloc] init];
NSLog(@"A: %i, B %i", [a add:100], [b add:b]);

Edit:
As the text I cited, the code above is supposed to cause errors, but it only produces some warning messages, Multiple methods named “add:” found, Incompatible pointer to integer conversion sending “id” to parameter of type “int”

I have Java and C++ background, I know polymorphism in Objective-C is slightly different from that of those languages but I am still confused about the uncertainty(text in bold).

I think I must have misunderstood something, would you please explain in more detail about dynamic binding in Objective-C for me and those who need it?

Thanks you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:02:48+00:00Added an answer on May 26, 2026 at 3:02 pm

    You haven’t noticed anything unusual because those two methods have the same call semantics under, for example, the x86_64 ABI. Pointers can be considered integers and, under the x86_64 ABI, they are passed to the target method in the same manner.

    However, if you had another class, e.g.:

    @implementation C
    - (int)add:(float)number {
        return (int)number + 100;
    }
    @end
    

    receiving a floating-point argument (as mentioned by Kochan), then the compiler, when parsing:

    id a = [[A alloc] init];
    id b = [[B alloc] init];
    id c = [[C alloc] init];
    NSLog(@"A: %i, B %i, C %i", [a add:100], [b add:b], [c add:100]);
    

    wouldn’t know that for [c add:100] it should place 100 in a floating-point register as specified by the x86_64 ABI. Consequently, -[C add:], which expects the floating-point argument to be in a floating-point register, reads a value that doesn’t correspond to the 100 argument.

    For it to work, you would have to either declare the variable with a static type:

    C *c = [[C alloc] init];
    

    or cast it to the correct type when sending a message:

    [(C *)c add:100];
    

    At the end of the day, sending an Objective-C message is a function call. Different ABIs may have different semantics for calling functions with variable arguments, floating-point vs. integer arguments or return values, or structures instead of scalar arithmetic types. If the compiler sees different method signatures that are handled differently according to the target ABI, and if there is not enough type information available, it may end up choosing the wrong method signature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning Objective-C and OOP ... so I have very stupid question
I have just started learning ruby reading from different resources. One of them is
I have just started learning objective-C and the iphone sdk and I have a
I have started learning Objective C with no prior programming experience from the 'Become
I just started learning objective c. I have this little problems and questions, hope
Hi Just started learning c for uni (usually use objective c) and have run
I have just started learning Erlang and am trying out some Project Euler problems
I have just started learning Jquery and am new to writing javascript (I am
I have just started learning MVVM and having a dilemna. If I have a
I have just started learning NHibernate. Over the past few months I have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.