Swift: print() vs println() vs NSLog()

转自 http://stackoverflow.com/questions/25951195/swift-print-vs-println-vs-nslog#

  1. println vs print:

    Prior to Swift 2, println would add a newline at the end of the results, whereas print would not.

    Starting in Swift 2, println is no longer used. One would generally use print (either without appendNewline parameter, or having that parameter set to true) to print a line followed with a newline character. You would use print with appendNewline of false if you want to print a string without a newline at the end.

  2. NSLog vs print/println:

    • NSLog is slower;

    • NSLog adds a timestamp and identifier to the output, whereas println will not;

    • NSLog synchronizes the log statements so that if you're issuing logs from different threads concurrently, they won't overlap with each other; println can result in jumbled output if performed simultaneously from separate threads without doing some synchronization (e.g. dispatching it to some serial queue, such as the main queue);

    • When performed on physical device, NSLog statements appear in the device's console whereas println only appears in the debugger console.

Generally in Swift, you'd use print/println, though you can use NSLog, when needed (e.g., it's critical that it appears in the console or if you're doing this from multiple threads and you don't want to have to synchronize this yourself). Either of these should be able to display your dictionary without incident.

you can pass in an NSString to println, but not NSLog; you can add args for NSLog, but not println; Swift style string interpolation sometimes crashes for NSLog, but not println.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值