加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:55:05 所属栏目:百科 来源:网络整理
导读:转自 http://stackoverflow.com/questions/25951195/swift-print-vs-println-vs-nslog# 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,241)">println is no l

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

  1. printlnvsprint:

    Prior to Swift 2,printlnwould add a newline at the end of the results,whereasprintwould not.

    Starting in Swift 2,241)">printlnis no longer used. One would generally useprint(either withoutappendNewlineparameter,or having that parameter set totrue) to print a line followed with a newline character. You would useprintwithappendNewlineoffalseif you want to print a string without a newline at the end.

  2. NSLogvsprint/println:

    • NSLogis slower;

    • NSLogadds a timestamp and identifier to the output,241)">printlnwill not;

    • NSLogsynchronizes the log statements so that if you're issuing logs from different threads concurrently,they won't overlap with each other;printlncan 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,241)">NSLogstatements appear in the device's console whereasprintlnonly appears in the debugger console.

Generally in Swift,you'd useprintln,though you can useNSLog,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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读