Hello I have the following code:
if(_deviceSegmentCntrl.selectedSegmentIndex == 0)
{
deviceOne = [[NSString alloc] initWithFormat:@"string01"];
}
if(_deviceSegmentCntrl.selectedSegmentIndex == 1)
{
deviceOne = [[NSString alloc] initWithFormat:@"string02"];
}
if(_deviceSegmentCntrl.selectedSegmentIndex == 2)
{
deviceOne = [[NSString alloc] initWithFormat:@"string03"];
}
NSString *deviceType = [[NSString alloc] initWithFormat:_deviceSegmentCntrl];
I am wanting to have the NSString output the string the user selects in the segmented control.
How do I append the initWithFormat: so that it reflects the chosen index?
Cheers
Firstly, NSString’s are so common that there is a shorthand to greatly simplify your instantiations:
is has an identical result to
Now, I’m not sure exactly what you are wanting to achieve, so I’ll mention a few things that I think are in the ballpark.
If you just want a string that is exactly what is displayed on the control, use
When you want a more complex string, you can use the class method
stringWithFormat, perhaps like this:Where deviceOne is from your posted code.