NSLog學習筆記 (一)

都知道用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,testnil];"就會出錯了。奇怪。


以下代碼先建立字符串對象,這樣就得到不受影響的結果:

    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>>"

)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值