Question

Je suis assez nouveau sur objective-c et j'ai du mal à comprendre le message d'avertissement pour le bloc de code suivant:

void PrintPathInfo() {
    NSString *path = @"~";  
    NSString *message = @"My home folder is: ";

    NSLog([message stringByAppendingString: [path stringByExpandingTildeInPath]]);
}

Voici le message d'avertissement que je reçois pour la dernière ligne (appel à NSLog):

warning: format not a string literal and no format arguments

Quelqu'un peut-il clarifier s'il vous plaît? Est-ce un message d'avertissement qui devrait m'inquiéter?

Merci.

Était-ce utile?

La solution

Votre code devrait fonctionner correctement, mais pourrait se comporter de manière erronée s'il existe des caractères de formatage '%' dans la chaîne transmise, ce qui pourrait confondre NSLog. Par exemple, essayez d’échanger ceci dans votre code:

NSString *message = @"My home %folder is: ";

NSLog interprétera ce '% f' de manière incorrecte.

Vous pouvez éviter l'avertissement (et le danger) en utilisant un littéral de chaîne avec le formatage et la transmission de vos chaînes, comme ceci:

NSLog(@"%@%@", message, [path stringByExpandingTildeInPath]);

Vous pouvez également vérifier ce lien:

http://www.cocoabuilder.com/archive / message / cacoa / 2009/8/29/243819

Bonne chance!

Autres conseils

Si vous voulez écrire le résultat dans nslog, vous avez besoin de quelque chose comme ceci:

NSLog(@"My home folder is %@",[path stringByExpandingTildeInPath]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top