Objective-c erreur EXC_BAD_ACCESS

Résolu/Fermé
G4Bb Messages postés 165 Date d'inscription samedi 20 mars 2010 Statut Membre Dernière intervention 6 août 2014 - 13 juin 2012 à 05:39
G4Bb Messages postés 165 Date d'inscription samedi 20 mars 2010 Statut Membre Dernière intervention 6 août 2014 - 13 juin 2012 à 17:20
Bonjour,

Je suis nouveau pour la programmation de l'objective-c et je reçois une erreur lorsque j'exécute mon petit programme (je veux seulement générer un caractère au hasard). J'ai donc fais jusqu'à maintenant :

- (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;
}

J'ai essayé de simplement mettre ma variable 'lettre' dans mon label hasard.text mais je reçois cette erreur : 'Incompatible interger to pointer conversion assigning to 'NSString *' from 'char'. Alors j'ai créé un NSMutableString pour contenir mon caractère. Lorsque je place la lettre "e" manuellement au lieu de la variable 'lettre' sur la 5e ligne de mon code, tout fonctionne parfaitement bien. Si je peux donc voir un caractère généré automatique dans la variable 'lettre' dans le déboggeur, pourquoi ais-je cette erreur? (EXC_BAD_ACCESS (code=2, address=0x42)).


Hi,

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 my Label 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)).

Thanks for your answers !

1 réponse

G4Bb Messages postés 165 Date d'inscription samedi 20 mars 2010 Statut Membre Dernière intervention 6 août 2014 64
13 juin 2012 à 17:20
J'ai eu la solution sur un autre topic, je vais quand même la poster au cas où quelqu'un chercherait.

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, characterAtIndex doesn't return a character as you would think. It returns a unicode character which is actually an unsigned short instead of an unsigned char. However, if you change your code to this it will work:

const unichar foo = [@"Test" characterAtIndex:0];
NSString *test = [NSString stringWithCharacters:&foo length:1]; //Note the &
EDIT But the simplest way would just be this:

char randChar = arc4random_uniform(26) + 'A'; //Changed in response to Jason Coco's comment
NSString *mot = [NSString stringWithFormat:@"%c", randChar];

De : borrrden, sur StackOverflow
1