I’m new in Objective-C programmation and I receive this error when I run my little program (I just want to generate a random char). So here’s what I’ve done so far:
(IBAction)generate{
int a = arc4random() % 26;
NSString * chaine = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char lettre = [chaine characterAtIndex:a];
NSMutableString * mot = [[NSMutableString alloc] initWithCharacters:lettre length:1];
hasard.text = mot;
}
I tried to simply put my variable ‘lettre’ in hasard.text but it won’t work with the error ‘Incompatible integer to pointer conversion assigning to ‘NSString *‘ from ‘char‘. So I created an NSMutableString to contain my character.
When I put the character “e” manually instead of the variable ‘letter’ on the fifth line it works well. Since I can see in the debugger that ‘lettre’ contains a random letter, why do I get the error in the title ?
(EXC_BAD_ACCESS (code=2, address=0x42)).
The function is expecting a pointer to a character, you are giving it a literal character. You need to create a pointer to it. Also,
characterAtIndexdoesn’t return a character as you would think. It returns a unicode character which is actually anunsigned shortinstead of anunsigned char. However, if you change your code to this it will work:EDIT But the simplest way would just be this: