开发者

NSLog NSString* print doesn't print

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] initWit开发者_JS百科hString: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?


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().


You should try :

NSLog(@"%@", [myObj identiteSimple]);


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜