都知道用NSLog可以輸出一些程序運行的信息,但原來可以直接NSLog一個陣列,如:
NSArray *tests = [NSArray arrayWithObjects:@"hello",@"this",@"is",@"a",@"test", nil];
NSLog(@"%@",tests);
會輸出:
2012-07-09 15:29:12.146 test20120709[15649:f803] (
hello,
this,
is,
a,
test
)
但如果將上面改為:
NSArray *tests = [NSArray arrayWithObjects:@"hello",@"this",@"is",@"a test", nil];
NSLog(@"%@",tests);
則輸出為:
2012-07-09 15:31:42.472 test20120709[15695:f803] (
hello,
this,
is,
"a test"
)
也就是說,一個空格鍵,使得它將@"a test"看作是一個字符串。但如果直接打"NSArray *tests = [NSArray arrayWithObjects:hello,this,is,a,test, nil];"就會出錯了。奇怪。
以下代碼先建立字符串對象,這樣就得到不受影響的結果:
NSString *string1 = @"hello, this is";
NSString *string2 = @"a test";
NSArray *tests = [NSArray arrayWithObjects:string1,string2, nil];
NSLog(@"%@",tests);
輸出:
2012-07-09 16:02:03.042 test20120709[15994:f803] (
"hello, this is",
"a test"
)
嘗試在陣列中加入非字符串NS對象:
NSString *string1 = @"hello, this is atest";
NSError *error = NULL;
NSArray *tests = [NSArray arrayWithObjects:string1,error, nil];
NSLog(@"%@",tests);
輸出為如下,注意只有一行,error對象仿佛不存在:
2012-07-09 16:04:59.255 test20120709[16026:f803] (
"hello, this is atest"
)
極端做法,在陣列中加入UI對象,結果十分有趣!
NSString *string1 = @"hello, this is atest";
UIButton *testButton = [[UIButton alloc] init];
[testButton setTitle:@"test" forState:UIControlStateNormal];
NSArray *tests = [NSArray arrayWithObjects:string1,testButton, nil];
NSLog(@"%@",tests);
輸出如下:
2012-07-09 16:07:57.158 test20120709[16093:f803] (
"hello, this is atest",
"<UIButton: 0x68afd80; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x68afe40>>"
)