1

I need to concatenate and display a string variable and a symbol in a Text view in SwiftUI, I basically need the effect you get when using string interpolation as follows...

Text("Some Text \(Image(systemName: "star"))")

enter image description here

I basically need to display text and a symbol but the text comes from a variable, something like the following which gives me an error...

    let someText = "Some Text"
    let someTextPlusImage = someText + Text(Image(systemName: "star")

    Text(someTextPlusImage)

Error:

error: Segmentation fault: 11

How can I concatenate and display a string variable and a symbol in a Text view in SwiftUI?

2
  • Have you heard of an HStack? Commented Aug 20, 2022 at 13:56
  • 1
    A String isn't a View You can + a String with a String and a Text with a Text but not a String with a Text Commented Aug 20, 2022 at 14:02

2 Answers 2

3

Insert like this

Text("\(someText) \(Image(systemName: "star"))")
Sign up to request clarification or add additional context in comments.

1 Comment

God, I tried "\(someText) \(Image(systemName: "star"))" but didn't work, I never tried Text("\(someText) \(Image(systemName: "star"))"). I did the trick. Thanks a lot.
3

Next variant also works (for example if needed in ForEach, etc.)

Text(someText) + Text(Image(systemName: "star"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.