Amazon.com Widgets Ex.9 Q.4 Note
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 27, 2014, 10:20:37 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
| |-+  Exercises
| | |-+  Chapter 9
| | | |-+  Ex.9 Q.4 Note
Pages: [1]   Go Down
Print
Author Topic: Ex.9 Q.4 Note  (Read 1308 times)
alanh
Newbie
*
Posts: 1






« on: February 04, 2012, 02:12:34 PM »

Hi Stephen,

In ex.9 q.4, you included such a note:
      
Quote
Note
You’ll have to change the name of the methods to something other than add:. That’s because the system’s NSObjectController class also has an add: method.
As noted in the “Arguments and Return Types with Dynamic Typing” section, if multiple methods of the same name exist in different classes and the type of
the receiver isn’t known at compile time, the compiler will perform a consistency check to make sure the arguments and return types are consistent among
the similarly named methods.

Q1: Wouldn't the add: method in NSObjectController be overrode by add: in Fraction and Complex classes?
Q2: Based on my program and others' on this forum, the same method add: for Fraction and Complex work for this question yes?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #1 on: February 04, 2012, 03:42:56 PM »

There's only a potential conflict if you use the add: method with an id object.   Since the receiver isn't statically declared in such a case, the compiler will check for consistency among all known declarations for the add: method (although that still doesn't guarantee that at runtime one of those methods will be chosen--it can be in a class the compiler hasn't encountered when processing the file that calls the add: method).

Cheers,

Steve
Logged
ljaygould
Newbie
*
Posts: 16






« Reply #2 on: February 05, 2012, 10:56:59 PM »

I have to say this one leaves me completely at sea. Even going back to the answers from 3rd Edition where apparently you switched the problem around and had the Complex class add: method switched around, I can't make sense of this. I can't make it work...everything I try

In Fraction.h:

-(id) addTo: (id) i; //so far so good, no errors

In Fraction.m //here all hell breaks loose!

The example in the 3rd Edition (Answers section) has the id variable being alloc/initialized as a Complex object. I don't get why. I tried to apply as Fraction and end up with a confusing mess.

I don't mean to criticize but I don't believe I have been given enough information to process this question within the chapter.

I have no clue how to initialize the (id) i  here. there is NO example in the chapter for putting an (id) into header files or doing this in such a way not to suddenly encounter a sea of red dots all over the place...I think I've read the chapter pretty carefully but this feels like I just got dropped into the middle of the Atlantic.

HELP!!!

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







« Reply #3 on: February 06, 2012, 10:10:51 AM »

You don't alloc an id object.  The object you alloc must always come from a particular class.  Then you send that alloc'ed object an init message to initialize it.  An id variable is used to store references to objects from different classes.   So you can alloc and init a Fraction object and store the result in an id variable, and you can do the same thing with a Complex object.

Cheers,

Steve
Logged
krislwright
Newbie
*
Posts: 7


Email




« Reply #4 on: May 21, 2012, 10:21:35 AM »

That's clear as mud. How about an example?
Logged
happyzhb
Administrator
Newbie
*****
Posts: 47


Email




« Reply #5 on: May 21, 2012, 10:04:18 PM »

Quote
I tried to apply as Fraction and end up with a confusing mess

Can you post your code here so we can see what you are trying to do and clear the mess for you?
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.