Quick question on programming a multiplication

Discussion in 'iOS Programming' started by LastLine, Jun 9, 2010.

  1. LastLine macrumors 65816

    Aug 24, 2005
    -(IBAction)donow {
    	NSString *start = input.text;
    	output.text = start * 1.1;
    Now this seems really simple, I can't quite see what I'm doing wroing.

    'donow' is started when a button is pushed, the value on a textbox (for example's sake called 'input' is stored in a string called 'start'
    I then want to put into a label called 'output' the value of 'start' multiplied (or added, divided, subtracted) by a set number.

    Can anyone suggest what the answer is here as this provides an error

    I'm also thinking I need to release start with [start release] but where do I put it?

    I know this seems really simple and I'm convinced it is as I can't actually find any examples to show me how to work it.
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    start is a string. You can't multiply strings. You have to convert the string to a numeric type, such as long or double, then perform the multiplication.

    You must then reverse the conversion, and convert you numeric value back into a string before assigning it to output.text.

    After solving that, if you still think you need to release start, you should reread the memory management guide and carefully review the rules of ownership. You should be able to answer the question "Do I own this?" and decide about release without having to ask anyone else. This is fundamental.
  3. Thomas Harte macrumors 6502

    Nov 30, 2005
    The problem is that an NSString is just an ordered sequence of symbols with no inherent additional meaning. You also can't apply direct mathematical operators to objects — Objective-C differentiates between primitive types (all the ones from C, such as float, short, int) and objects (which are always kept track of by pointers, so have names beginning with an asterisk such as your object 'start'). Operators like *, /, + and - work on primitive types but not on objects.

    What you can do is use either the 'floatValue' or 'doubleValue' method to get the Objective-C runtime to inspect the string and figure out what value it would represent as a number. Then you can multiple that by 1.1. Then you can use the runtime's ability to create strings that represent how numbers would look to a human to output the value.

    That'd be (in very expressive code):

    	NSString *start = input.text;
    	double startValue = [start doubleValue];
    	double endValue = startValue * 1.1;
    	NSString *endText = [NSString stringWithFormat:@"%0.2f", endValue];
    	output.text = endText;
    Because start is an object, you'd expect it to know how to do operations on itself, so it performs doubleValue. Because endValue is a primitive type, not an object, it doesn't have any intelligence and you have to ask some other object (in this case NSString) to apply the intelligence to operate on it.
  4. LastLine thread starter macrumors 65816

    Aug 24, 2005
    Ah yes, of course string's are essentially items rather than numbers. Thanks for that, it's actually a big help.

    And yeah, the memory management needs a reread on my part, just trying to get a bit of advice ;-)

    EDIT: I actually want to add on here this isn't me trying to create an application for release, this is merely me exploring how to do things in a random application - learning for the sake of learning on this one.

Share This Page