Help retrieving values from UIPicker

Discussion in 'iOS Programming' started by ideviant, Jun 23, 2011.

  1. ideviant, Jun 23, 2011
    Last edited by a moderator: Jun 23, 2011

    ideviant macrumors newbie

    Jun 23, 2011
    i'm using a UIPicker, it has 2 component and i'm having problem adding the 2 values from the component together.

    my codes are here:
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    	for (int i =0; i <2; i++)
    		double tenth = [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]];   
    		double oneth = [array2 objectAtIndex:[pickerView selectedRowInComponent:1]];
    	double result = [ double oneth + (double tenth * 10) ]; 
    and i have to displayed the result out on to my label called testlabel.
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    1) Please wrap your code in [ code ] [ /code ] (without the spaces) tags.

    2) What is arrayNo? Can I assume it's a NSArray (given that you are using objectAtIndex:)? If so it cannot ever return a double: it can only return an object.
  3. ideviant thread starter macrumors newbie

    Jun 23, 2011
    sorry, i do not understand the wrap code in
    i'm using NSMutableArray.
  4. ideviant thread starter macrumors newbie

    Jun 23, 2011

    so sorry i was new to this.
    and i was desperately looking for answer as it's for my assignment which is going to be due on monday and my group members are not doing anything about it. all they know is answer i dont know.
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    There are several things wrong here:

    1. You don't need a loop.
    There is no reason for a loop of any kind. You're just repeating the exact same statement multiple times. You're not even using the variable i in the loop body.

    2. objectAtIndex: doesn't return a double.
    When arrayNo is an NSArray or NSMutableArray, the objectAtIndex: method returns an object (typename id). It doesn't return a double. So assigning an object to a double is completely wrong. The compiler should generate a warning about this.

    3. The variable tenth only exists inside the loop body.
    When you define a variable inside any block delimited by { }, that variable name is only visible between that set of { }. It is inaccessible outside the block. The significance of this comes later in the code.

    double oneth = [array2 objectAtIndex:[pickerView selectedRowInComponent:1]];
    You have a similar error here -- objectAtIndex: returns an id type, not a double, so assigning it to a double is wrong.

    You also used a different array name (array2) than your earlier code, which used arrayNo. I can't tell if this is an error or it's intentional. Without knowing more details about what the contents of the arrays are, all anyone can do is guess.

    double result = [ [COLOR="Red"]double[/COLOR] oneth + ([COLOR="Red"]double[/COLOR] tenth * 10) ]; 
    Several things are wrong here:

    1. The [ ] surrounding the expression are wrong.
    When [ ] are arranged like that, they signify a message-send to an object. The [ ] DO NOT mean "outer level of parentheses" as they would in a schoolroom math class.

    2. The red-hilited double's shouldn't be there at all.

    3. There is no tenth variable accessible from this point.
    You defined the variable inside the for body. That means it's inaccessible outside that {}-delimited block.

    Overall, this looks like you're making up code with very little study of the fundamentals, and no guidance like you'd get from an actual book or tutorial. Making stuff up and hoping it works is one of the worst ways to learn how to code.
  6. ideviant thread starter macrumors newbie

    Jun 23, 2011
    sorry, i wasn't taught on any fundamental and etc, i was just given the project and expected it to be done by tonight.
    and for the coding, i'm left with changing the arrayobject into a datatype and putting it in the detailedview cell. hope you guys can help me out.

Share This Page