Reputation: 1095
I am having problems with NSLog method and some objects. My objects return an NSString*, but the string is concatenated with several other strings like this :
-(NSString*) identiteSimple
{
NSString *res = [[NSString alloc] initWithString:prenom];
res = [res stringByAppendingString:@" "];
res = [res stringByAppendingString:nom];
return res;
}
however, when I try to print it using NSLog like this :
NSLog([myObj identiteSimple]);
Xcode warns me that:
"Format string is not a string literal (potentially insecure)"
And nothing gets printed. Does anyone have any idea why the NSString doesn't get printed?
Upvotes: 1
Views: 5245
Reputation: 13791
Based on your comments to other answers, you're not actually allocating memory for your Personne
object.
Personne *moi = [moi initWithName:@"Lennon" prenom:@"John" civilite:@"Mr"];
moi
is nil
when you try to call initWithName:prenom:civilite:
. Messages to nil
do nothing and return nil
, so moi
will still be nil
, and identiteSimple
will also return nil
when you call NSLog()
.
Upvotes: 4
Reputation: 46027
First of all, you are leaking memory for res
. You are allocating that, then pointing it to new string without releasing the alloced one.
NSString *res = [[NSString alloc] initWithString:prenom]; // after this line res points to new string, leaking previous alloced memory res = [res stringByAppendingString:@" "];
Instead of alloc you should create autoreleased string here.
NSString *res = [NSString stringWithString:prenom]; res = [res stringByAppendingString:@" "]; res = [res stringByAppendingString:nom];
Or even beteter:
return [NSString stringWithFormat:@"%@ %@", prenom, nom];
My guess about not printing is that prenom
or nom
is not a valid NSString. You should post their code too. And you can debug by checking res
after every new string to find out where it is breaking.
Something like this:
NSString *res = [NSString stringWithString:prenom]; NSLog(prenom); // prenom is OK NSLog(res); // res is OK here res = [res stringByAppendingString:@" "]; NSLog(res); // res is OK here too res = [res stringByAppendingString:nom]; NSLog(nom); // nom is OK NSLog(res); // final content of res
Hope it helps.
Upvotes: 1