I know how to programmatically do it, but I'm sure there's a built-in way...
Every language I've used has some sort of default textual representation for a collection of objects that it will spit out when you try to concatenate the Array with a string, or pass it to a print() function, etc. Does Apple's Swift language have a built-in way of easily turning an Array into a String, or do we always have to be explicit when stringifying an array?
Result will be >>> Mani Singh iOS Developer
The Swift equivalent to what you're describing is string interpolation. If you're thinking about things like JavaScript doing
"x" + array
, the equivalent in Swift is"x\(array)"
.As a general note, there is an important difference between string interpolation vs the
Printable
protocol. Only certain classes conform toPrintable
. Every class can be string interpolated somehow. That's helpful when writing generic functions. You don't have to limit yourself toPrintable
classes.Swift 3
Swift 2.0 Xcode 7.0 beta 6 onwards uses
joinWithSeparator()
instead ofjoin()
:joinWithSeparator
is defined as an extension onSequenceType
You can print any object using the print function
or use
\(name)
to convert any object to a string.Example:
In Swift 2.2 you may have to cast your array to NSArray to use componentsJoinedByString(",")