Welcome, Guest. Please login or register.
Did you miss your activation email?
July 23, 2016, 04:08:51 AM
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
| | |-+  About dynamic typing , instance/class method
Pages: [1]   Go Down
Author Topic: About dynamic typing , instance/class method  (Read 2667 times)
Posts: 3

« on: March 11, 2012, 07:45:37 PM »

I'm new to obj-c. I'm currently reading Chapter 9 in this book (4e, 1st printing), and don't know whether my questions are answered in the later chapters.

Program 9.2:
Code: (Objective-C)
int main (int argc, char * argv[])
   @autoreleasepool {
      id    dataValue;
      Fraction *f1 = [[Fraction alloc] init];
      Complex  *c1 = [[Complex alloc] init];
      [f1 setTo: 2 over: 5];
      [c1 setReal: 10.0 andImaginary: 2.5];
      // first dataValue gets a fraction
      dataValue = f1;
      [dataValue print];
      // now dataValue gets a complex number
      dataValue = c1;
      [dataValue print];
   return 0;

Program 9.2 is supposed to work without any warning/error. But anther example in Chapter 9

Code: (Objective-C)
id dataValue = [[Fraction alloc] init];
[dataValue setReal: 10.0 andImaginary: 2.5];

is supposed to produce a warning message.
I wonder why there's difference between these 2 examples ? They both assign a Fraction object to an id variable.

There's a method of NSObject:

-(BOOL) respondsToSelector: selector

In Chapter 9

The test

if ( [Square respondsToSelector: @selector (alloc)] == YES )

tests whether the class Square responds to the class method alloc, which it does because it’s inherited from the root object NSObject. Realize that you can always use the class name directly as the receiver in a message expression, and you don’t have to write this in the previous expression
(although you could if you wanted):

[Square class]

That’s the only place you can get away with that. In other places, you need to apply the class method to obtain the class object.

Since respondsToSelector is an instance method, why is it able to be used to send message to an class object?
And it says we can use class name instead of class object here. Is it only a  exceptional shortcut, or is there any mechanism behind this?

Thanks in advance!
Hero Member
Posts: 3114

« Reply #1 on: March 12, 2012, 02:30:36 PM »

In the first case, dataValue is statically typed, and the compiler knows the methods in the Complex class.  In the second case, dataValue is an id object, and the complier doesn't know the class of the object it will reference, so it can't do any checking at all.

As for the respondsToSelector: question,  I never realized the issue of that being an instance method (although it's declared in a protocol of NSObject, and you'll get a warning if you declare your class to conform to the NSObject protocol).  It appears to be some type of special case, but I haven't quite tracked that down yet.  A discussion on stack overflow (http://stackoverflow.com/questions/1135366/class-method-equivalent-of-respondstoselector) was equally unenlightening.  I'll research this further.



Posts: 1

« Reply #2 on: March 18, 2013, 03:14:31 AM »

Dear Kochan,

I am confused about your reply to the first question of starfall. dataValue is declared as id in both programs, but why it is static type in program 1 but dynamic type in program 2.

For the second question, isn't it because class object is also an object, thus allowing it to be the receiver of the method call, providing that the method is invokable for that object?

Many thanks,
Poor Man.
Pages: [1]   Go Up
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.