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

转自 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.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...