tags:

views:

122

answers:

3

This function is not executed well.

-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
    NSLog(@"A");
    NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
    NSLog([ary description]);
    NSLog(@"B");
}

log

[Session started at 2009-11-07 20:46:10 +0900.]
2009-11-07 20:46:19.170 xxx[2374:207] A

What is the cause?

+6  A: 

It is generally unwise to pass nonconstant format strings into NSLog, things get wonky. Try:

-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
    NSLog(@"A");
    NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
    NSLog(@"%@", [ary description]);
    NSLog(@"B");
}
Louis Gerbarg
I tried. But it not executed.
marcy
You could state more clearly that it is commonly regarded as a security leak to use non-literals as format strings and in this case plain wrong. It does not have to do with constant vs. non-constant.
Nikolai Ruhe
Nikolai: That is true for c strings, and the security issue is equally valid for Objective C. In addition to that there are actually other problems that occur with NSLog due to ducktyping and people passing in things that are not what they think they are. IOW, ignoring the security issue, sometimes just passing an object as the first arg of an NSLog doesn't work.
Louis Gerbarg
Err, an object that is not a CFConstantString (which is the underlying type emitted by the compiler for literal NSStrings or CFSTR()'s
Louis Gerbarg
Don't use NSLog(@"%@", [ary description]) - just use NSLog(@"%@", ary);
AlBlue
Do you say that using a valid, carefully calculated CFMutableString (or even CFString) for the format argument of NSLog does not work as expected? I did not hear of that before.
Nikolai Ruhe
B.t.w.: The security issue is a little less grave with NSLog than with printf because NSLog does not support the evil %n conversion specifier.
Nikolai Ruhe
I have definitely passed NSString's directly into NSLog and seen them not come out the other side, though I suppose there is also a chance that was a bug in a pre-release SDK or something that I just internalized.
Louis Gerbarg
I'd love to hear more about that. If you ever find out what happened or have evidence of this bug, maybe you want to write about it on your blog. Thanks for the ZFS article b.t.w., great read!
Nikolai Ruhe
A: 

I tried. But it not executed.

-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
    NSLog(@"A");
    NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
    NSLog(@"%@", [ary description]);
    NSLog(@"B");
}

log

[Session started at 2009-11-07 21:25:37 +0900.]
2009-11-07 21:25:48.738 xxx[2455:207] A
marcy
Well, it works just fine for me, so your errors are elsewhere. What parameters are you passing into the method? Are you sure that it is being called at all (do you have an NSLog(@"A");) somewhere else in your code.
Louis Gerbarg
outis
A: 

I change NSArray to NSMutableArray. It is executed.

-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
    NSLog(@"A");
    NSArray* ary = [[NSMutableArray alloc] init];
    [ary addObject:a];
    [ary addObject:b];
    [ary addObject:c];
    NSLog(@"%@", [ary description]);
    NSLog(@"B");
}
marcy