Help with unused variable and NSString may not respond

Discussion in 'iOS Programming' started by ericb8272, Apr 3, 2010.

  1. ericb8272 macrumors newbie

    Joined:
    Apr 3, 2010
    #1
    I am new to programming and to the iPhone. I am trying to recreate an app that I found in a book on iPhone programming as practice before I attempt to make an original app. This app has a slider and a label beside the slider that should change when the slider is moved. I am receiving 2 errors and a warning on 2 lines of code. Unused variable 'percentage' on one line and NSString may not respond to +stringWith and expected ] before format on another line. I'm sure this is something simple but I'm stuck and have no one to ask for help. Any help would be greatly appreciated.


    Code:
    // determine if customPercentSlider generated the event
    		else if (sender == customPercentSlider)
    		{
    			// the "Custom" slider was moved
    			// round the value to a whole number
    			int percentage = (int)(customTipPercent * 100);
    			
    			//update the label with the new percentage followed by %
    			customPercentLabel.text = 
    				[NSString stringWithFormat:@"%i%%", percentage];
    			
    			// convert percentage back to float and assign to Slider's value
    			float newSliderValue = newSliderValue;
    			customPercentSlider.value = newSliderValue;
    			
    			// slider Thumb moved; update customTipPercent
    			customTipPercent = newSliderValue;
    		} // end else
    
    The first error is on the line that starts int percentage. The next 2 are on the line [NSString stringWithFormat:mad:"%i%%", percentage];
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    I don't see any error in the code you posted. Try moving the two lines onto one line (although it should be ok on two lines as you showed it). The compiler seems to think there's a space or line break between stringWith and Format but there is none in the code you showed.

    Code:
    customPercentLabel.text = [NSString stringWithFormat:@"%i%%", percentage];
    
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  4. ericb8272 thread starter macrumors newbie

    Joined:
    Apr 3, 2010
    #4
    Thanks for the responses. I tried your suggestion and it still did not clear the errors.

    The first error is unused variable 'percentage'

    Code:
    int percentage = (int)(customTipPercent * 100);
    The next two are expected ']' before 'Format' and 'NSString' may not respond to '+stringWith'

    Code:
    customPercentLabel.text = [NSString stringWithFormat:@"%i%%", percentage];
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Yes, you already said what the errors were. And as I said that compiler thinks there's a break between the 'stringWith' and the 'Format'.

    Try deleting that line from your source file and typing it again.
     
  6. ericb8272 thread starter macrumors newbie

    Joined:
    Apr 3, 2010
    #6
    Thank you very much! This morning I deleted the lines of code and re-entered them, as you suggested. It worked and the app worked perfectly in the simulator. Again thanks.
     

Share This Page