Amazon.com Widgets Exercise 9.4 Confused
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 31, 2014, 05:02:39 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
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
| | | |-+ Exercise 9.4 Confused
Pages: [1] Go Down
Print
Author Topic: Exercise 9.4 Confused (Read 1703 times)
mark.pedigo
Newbie
*
Posts: 5


Email




on: October 28, 2011, 02:03:43 PM

Hi,

I'm working on Exercise 9.4. We're to modify the add method to take and receive ids instead of Fractions.

The original method
-(Fraction *) add:(Fraction *)f
{
    // To add two fractions:
    // a/b + c/d = ((a*d)+(b*c))/(b*d)
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator + denominator * f.numerator;
    result.denominator = denominator * f.denominator;
   
    [result reduce];
    return result;
}

I don't understand how to take an id as an argument instead of a fraction. If we change f to an id, then how do we know a priori that it would have denominator and numerator fields? Wouldn't that only apply to Fraction objects? So,

-(id) addID:(id)f  // Changed add to addID to avoid namespace clash re: the note.

isn't going to work because f.denominator now makes no sense. I could take an id f and set it to a Fraction object, but then I'd have to tell the method about a specific Fraction, which defeats the purpose of the id. I seem to be in a catch-22.

Am I missing something here? Am I reading this question wrong?

Thanks in advance for any help.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: October 28, 2011, 03:25:09 PM

Because of dynamic typing (covered in Chapter 9), the compiler doesn't need to know what methods are supported by an object.  So you can write [f numerator] or [f denominator] and the right method will be selected at runtime.   Note that you can't use the dot operator with id variables.  So you have to write [f denominator] and can't write f.denominator.

Cheers,

Steve
Logged
mark.pedigo
Newbie
*
Posts: 5


Email




Reply #2 on: October 28, 2011, 04:57:08 PM

Thanks for the quick response. I think I get the answer, but I don't understand the whys.

So the . notation is only for objects that are known at compile time, and dynamic objects must use [] notation? Is that right?

What exactly is the difference between [] and ? Clearly, they're not interchangeable.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: October 28, 2011, 06:58:18 PM

There is no difference other than the restriction of not being able to use the dot operator with an id object.  I never figured out exactly why that is--I can only surmise that it's some compiler issue.   

Cheers,

Steve
Logged
mark.pedigo
Newbie
*
Posts: 5


Email




Reply #4 on: October 28, 2011, 09:10:47 PM

Yokay, I can live with that. I'll accept it as an idiocyncracy of O-C.

BTW: Thanks for a great book and fantastic forums. I'm enjoying this quite a lot. The exercises are good: most of them are straight-forward, but there are a few (like this one and the overlapping rectangles one from Chapter 8 ) that really make me stop and think. I also appreciate that you tacitly assume we're capable of some math, instead of dumbing it down. (I'm a math teacher/"almost prof").

Mark
Last Edit: October 28, 2011, 09:21:50 PM by skochan Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: October 28, 2011, 09:21:22 PM

Hi Mark,

Thanks for your generous remarks.   Wait until you see Exercise 9 in Chapter 15....that's the best/hardest in the book. 

I have gotten some knocks for the use of math in the book, but it's the best way to introduce concepts like variables (without getting into objects) and algorithms, which are an important part of programming.  I went to a HS in NYC that specialized in math & science (and at one time even considered majoring in math), so I have a fairly strong math background as well and just assumed the readers would be okay with things like fractions.  I get the impression that students coming out of school (or still in school) may not be getting as good a math education these days, but that's just based on comments about my book.

Cheers,

Steve
Logged
mark.pedigo
Newbie
*
Posts: 5


Email




Reply #6 on: October 29, 2011, 11:34:05 PM

Some interesting posts on dot vs. bracket notation:

First, Apples's documentation: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html
("Dynamic Typing", "Dot Notation" for starters.)

This one, which seems to agree: http://eschatologist.net/blog/?p=160

And this one, which says "Don't do it, man!" http://eschatologist.net/blog/?p=160

The comments in the last two posts are worth reading, too.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #7 on: October 30, 2011, 10:24:45 AM

Thanks for the links.  The last noted one (which, by the way is the same as the 2nd you posted), is well-written.  It actually helped me understand why id vars don't work with the dot operator (I think--although you would write var.isHidden in the example cited and presumably that would be the underlying method name--although perhaps that's not how the property name/selector method mapping works).

Cheers,


Steve
Last Edit: October 30, 2011, 10:29:38 AM by skochan 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.