Amazon.com Widgets Objective-C runtime system check question
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 31, 2014, 10:45:39 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 9
| | |-+ Objective-C runtime system check question
Pages: [1] Go Down
Print
Author Topic: Objective-C runtime system check question (Read 1170 times)
dharr19
Full Member
***
Posts: 175


Email




on: February 02, 2012, 11:37:01 AM

Hi Stephen,

In chapter 9 page 184 Argument and Return Types with Dynamic Typing you bring up the following point.

if dataValue1 and dataValue2 are id variables then

result = [dataValue1 add: dataValue2];

cause the compiler to generate code to pass the argument add: method and handle its returned value by making assumptions.

but that in a more general case, the compiler might generate incorrect code to pass arguments to a method or handle its return value if one method took an object as its argument and the other took a floating-point value.  Or if one method returned an object and the other an integer;

I'm not sure what you meant. That is why different return types or argument types might cause incorrect code to be generated.

thanks
David
Last Edit: February 02, 2012, 11:39:27 AM by dharr19 Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: February 02, 2012, 03:15:34 PM

Sorry, that part isbn't worded so well and the compiler is apparently doing more checking now than it has in the past.   Let's try again:

If the compiler doesn't see the definition of a method before encountering the method call, it will assume the method returns a value of type id and will assume the method takes as its arguments types whatever you have specified for the arguments in the method call.   

If it later (in the same source file) encounters the same method being used, it will check for consistency of argument and return types and give an error if there's an inconsistency.      If it finds the definition for the same method name in multiple classes, and the method is being used with an id object, it will also check for consistency among the known definitions, since it won't know which method will be called until runtime. 

It's a little confusing.  Hope this helps.  I'll add some updated wording in the next printing.

Cheers,

Steve
Logged
dharr19
Full Member
***
Posts: 175


Email




Reply #2 on: February 03, 2012, 05:03:53 PM

 "If the compiler doesn't see the definition of a method before encountering the method call, it will assume the method returns a value of type id and will assume the method takes as its arguments types whatever you have specified for the arguments in the method call. " 

Does this means that it will just except it during compile time but will reject it during runtime unless it finds another definition at which time it will use those definitions to check for consistency?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: February 03, 2012, 07:37:53 PM

No, at runtime the system will look at the class of the object receiving the message and use the method from that class (or inherited by that class).  Hopefully, the compiler will have made the right assumptions (if that's the case) about the argument and return types will match the method that's called.

Cheers,

Steve Kochan
Logged
dharr19
Full Member
***
Posts: 175


Email




Reply #4 on: February 04, 2012, 08:45:05 PM

Ok,

I think I get it but to make sure I'm on the right page, what exactly does it mean if the compiler doesn't see the definition of  a method before encountering the method call?

Thanks
David
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: February 05, 2012, 01:48:43 AM

When compiling a particular file, it means any methods (if any) that are explicitly declared in that file, or imported from other files.  Recognize that importing Foundation.h effectively exposes the declaration of all methods from all classes in the Foundation framework.

Cheers,

Steve
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.