Brian Anderson
Brian Anderson

Reputation: 5

Convert char to int to NSString

I am writing some code to allow users to answer multiple choice questions. So I have an NSArray of values [@"value1", @"value2", ..]

I want to display them as: A) value1 B) value2

The code I have is

for(int i = i; i < [values count]; i = i+1) {
NSString *displayValue = [[NSString alloc] initWithString:<NEED HELP HERE>];
displayValue = [displayValue stringByAppendingString:@") "];
displayValue = [displayValue stringByAppendingString:[values objectAtIndex:i];
}

The question I have is if there is where I have put , how could I convert i to the right ASCII character (A, B, C, etc) and initialize the string with that value

Upvotes: 0

Views: 1059

Answers (2)

mvds
mvds

Reputation: 47034

NSString *displayValue = [NSString stringWithFormat:@"%c",'A'-1+i];

and to get the whole string at once, use:

NSString *displayValue = [NSString stringWithFormat:@"%c) %@",'A'-1+i,
                                                   [values objectAtIndex:i]];

(ps. if you alloc an object, you must also release or autorelease, or you will "leak" memory)

Upvotes: 1

Noel M
Noel M

Reputation: 16116

Look at NSString:initWithFormat method, along with the String Programming Guide.

Upvotes: 0

Related Questions