Reduce Array to String for Text and Other Views

If you ever need to concatenate an array of strings for formatting in SwiftUI view, you can use the reduce() function, like this:

let words = ["Hello", "World"]

var body: some View {
    words.reduce(Text(""), { $0 + Text($1) + Text(" ")} ) }

$0 = First parameter of reduce function
$1 = Reduce string

Jun 9th, 2020 • Posted in Apple, iOS, SwiftUI
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>