开发者

Convert char to int to NSString

开发者 https://www.devze.com 2023-01-10 11:50 出处:网络
I am writing some code to allow users to answer multiple choice questions. So I have an NSArray of values [@\"value1\", @\"value2\", ..]

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 valu开发者_运维技巧e


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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号