1

I have a NSSlider and an NSLabel. When the slider's value changes it changes the label's text to the new value. My problem is that I want to a "%" at the end, but whenever I try to change my code, error occurs.

Here's my code:

int sliderValue = [_opacSlider intValue];
NSString *stringValue = [NSString stringWithFormat:@"%i",sliderValue];
[_opacLabel setStringValue:stringValue];

What I attempted doing was this:

[_opacLabel setStringValue:(stringValue, @"%")];

But then the label doesn't change and remains "%".

Any ideas why this is not working and what I should change?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Lae
  • 832
  • 1
  • 13
  • 34

1 Answers1

1

Since stringWithFormat: method interprets % as a special character, you need to use %% to print a single %:

int sliderValue = [_opacSlider intValue];
NSString *stringValue = [NSString stringWithFormat:@"%i%%",sliderValue];
//                                                     ^^
[_opacLabel setStringValue:stringValue];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523