CurrencyConverter Pointer Question

Discussion in 'Mac Programming' started by rickb, Dec 5, 2006.

  1. rickb macrumors newbie

    Joined:
    Dec 5, 2006
    #1
    I'm getting an error in currency converter, while I'm sure I typed something wrong somewhere I want to understand my problem better

    my ConverterController.m is:

    #import "ConverterController.h"

    Code:
    @implementation ConverterController
    
    - (IBAction)convert:(id)sender
    {
    	float rate, currency, amount;
    	currency = [dollarField floatValue];
    	rate = [rateField floatValue];
    	amount = 8;//
    	id temp = [converter convertCurrency:currency atRate:rate];
    	[amountField setFloatValue:amount];
    	[rateField selectText:self];
    }
    
    @end

    The problem was that I was getting an error on setting the amount field, in the debugger I realized that

    [converter convertCurrency:currency atRate:rate];

    was returning a pointer, not a float? Which is why I can see it works if I explicitly create a float instead of a pointer to a float. Also would love to know what I did wrong.

    My Converter.m is:
    Code:
    @implementation Converter
    - (float)convertCurrency:(float)currency atRate:(float)rate {
    	float total = currency * rate;
    	return total;
    }
    
    I am fluent with java but I'm not sure I understand how the demo was supposed to originally work if the setAmount field expects a float but gets a pointer. Then again in Java, pointers/types are much simpler.

    Also, this is an example of messaging right, instead of explicit methods? It seems for a simple demo messaging would be more inherent to tough to spot problems.

    thanks
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Can you zip up the project and post that (without the compiled binaries)?
     
  3. rickb thread starter macrumors newbie

    Joined:
    Dec 5, 2006
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Fixed a few warnings in your project.

    Converter.h should look like this:

    Code:
    /* Converter */
    
    #import <Cocoa/Cocoa.h>
    
    @interface Converter : NSObject
    {
    }
    
    - (float)convertCurrency:(float)currency atRate:(float)rate;
    
    @end
    The method declaration was a bit off, although it shouldn't have made a difference.

    And then reverting convert: back to its original state:

    Code:
    - (IBAction)convert:(id)sender
    {
    	float rate, currency, amount;
    	currency = [dollarField floatValue];
    	rate = [rateField floatValue];
    	amount = [converter convertCurrency:currency atRate:rate];
    	[amountField setFloatValue:amount];
    	[rateField selectText:self];
    }
    Everything works fine.
     
  5. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #5
    Why do you have
    Code:
    id temp = [converter convertCurrency:currency atRate:rate];
    instead of
    Code:
    float temp = [converter convertCurrency:currency atRate:rate];
    ?

    Your method is returning a float, but you're the one converting it to a pointer!
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Looks like your problems have been solved whilst I was getting home from work :D
     
  7. rickb thread starter macrumors newbie

    Joined:
    Dec 5, 2006
    #7
    Thanks! aaaaa, makes sense...sort of.

    How would I write the call to converter explicitly without the messaging paradign?
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    You wouldn't. There are ways to call into the runtime directly to pass the message,but this is OO programming, we send messages.
     

Share This Page