2009-11-07 22 views
-3

該函數執行不好。objective-c函數執行不好

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

日誌

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

的原因是什麼?

編輯:

我試過了。 但它沒有執行。

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

日誌

[Session started at 2009-11-07 21:25:37 +0900.] 
2009-11-07 21:25:48.738 xxx[2455:207] A 
+2

你是什麼意思「不好執行」? – outis

回答

6

它通常是不明智的,非恆定的格式字符串傳遞到的NSLog,事情就靠不住。嘗試:

-(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"); 
} 
+0

我試過了。但它沒有執行。 – marcy

+0

您可以更清楚地表明它通常被認爲是使用非文字作爲格式字符串的安全漏洞,並且在這種情況下顯然是錯誤的。它不需要處理常量與非常量。 –

+0

Nikolai:對於c字符串是這樣的,安全問題對於Objective C同樣有效。除此之外,實際上還有其他問題是由於ducktyping和人們傳遞非他們認爲他們的東西。 IOW,忽略安全問題,有時只是傳遞一個對象,因爲NSLog的第一個參數不起作用。 –

-2

我將NSArray更改爲NSMutableArray。 它被執行。

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