Tutorial :NSString Converting into a doubleValue after extracting NSString from UIPicker



Question:

I have :

    NSInteger dollarrow = [ValuesPicker selectedRowInComponent:kDollar];        NSString *dollar = [dollarlist objectAtIndex:dollarrow];       double converteddollar=0;  

I want to do a for loop on this and get the value in double , I am trying this :

    for(dollar=1;dollar<=99;dollar++)      {      converteddollar = converteddollar + 2;      }  

Now the dollar contains NSString how do I convert into double to do the operation successfully and then

NSString *message = [[NSString alloc] initWithFormat:@"%0.2f",converteddollar];  

I am getting warnings and the app is crashing .. How can I correct it . I am learning Objective C please help . Thankyou . Sorry I did not want == i just wanted to use =


Solution:1

Your for loop is wrong. Are you really want '=='? I guess you want only '='. Can't you use something like this? (I'm just giving an example to point the mistakes in loop. Your requirement may be something different. And also I have not compiled the code, don't have mac with me right now. So you may get some minor mistake)

  double sum = 0    for (NSInteger i = 1; i &lt= 99; i++) {      sum += 2;  // or whatever you need here  }    NSString *convertedStr = [NSString stringWithFormat:@"%lf", sum];  


Solution:2

Lots of errors...

NSInteger dollarrow = [ValuesPicker selectedRowInComponent:kDollar];  

The selectedRowInComponent: method is a member method. Not a static one. You need to call it on an instance.

== is a comparison operator. What you want is certainly an assignment, with a single =sign.

You declare a pointer to a NSString object. But it's not initialized.
You should also use a temporary double variable for your loop.

I don't understand what your loop is supposed to do. Can you explain a little bit?


Solution:3

NSString *dollar = [dollarlist objectAtIndex:dollarrow];

double converteddollar = [dollar doubleValue]; should work (no need for you for loop)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »