可以直接在Swift中使用println作为String输出吗?

时间:2015-01-13 18:33:51

标签: swift

我非常好奇,如果我可以直接在我的界面中使用println,这将减少我向客户展示原型的时间,而无需编写特定的函数来迭代复杂的数据。 换句话说,我想要实现的是:

textView.text = println(object)

我很欣赏任何奇妙的方法! 问候。

2 个答案:

答案 0 :(得分:3)

toString()函数返回println()将打印到控制台的相同内容:

let xs = [1, 2, 3, 4, 5]
let xsString = toString(xs)
// xsString == "[1, 2, 3, 4, 5]"

在您的情况下,textView.text = toString(object)会做您想做的事。

答案 1 :(得分:1)

您也可以使用.description

let myArray = [1, 2, 3, 4, 5]
let myArrayDescription = myArray.description // "[1, 2, 3, 4, 5]"
let myDateDescription = NSDate().description // "2015-01-13 18:57:15 +0000"
// you can also use it with current locale
let myDateDescriptionWithLocale = NSDate().descriptionWithLocale(NSLocale.currentLocale())! // "Tuesday, January 13, 2015 at 4:59:07 PM Brasilia Summer Time"
相关问题