If print(x)
gives you what you want, use String(describing:)
. This is almost always the correct answer. (Generally speaking, this will be equivalent to "\(x)"
. "\(x)"
They do not have to be the same, but I have not yet found a case where they are not.) If only debugPrint()
gives you what you want, then use String(reflecting:)
.
Be very careful with these results. They are not localized and are not promised to remain consistent between releases. They are not a serialization API. There is no promise that they can be canceled to provide you with the original data. The result of these enumeration methods has changed dramatically in Swift 3. I absolutely expect them to change again, since Foundation will be enhanced for Swift.
I do not want to scare you about these methods. String(describing:)
pretty well defined as to what it returns in some cases (especially user types that implement certain protocols), but not in all cases. You must read the documents and use reasonable care. String(reflecting:)
, on the other hand, is clearly "suitable for debugging." I would not argue about this line.
source share