Search code examples
swiftstringimage-literals

swift 4 add a image literal to a string


I want to be able to add an image to a string with some text. There was an example video on headerViews and he included the data that had the images embedded in the string.

I am not sure how he did it.

Here is the data

Section(genre: "💥 Superhero",
        movies: ["Guardians of the Galaxy", "The Flash", "The Avengers", "The Dark Knight"],
        expanded: false,
        subtitle: "Please select a movie"),
Section(genre: "👻 Horror",
        movies: ["The Walking Dead", "Insidious", "Conjuring"],
        expanded: false,
        subtitle: "Please select a movie")

Section is a simple data structure:

struct Section {
    let genre: String!
    let movies: [String]!
    let expanded: Bool!
    let subtitle: String!    

    init(genre: String, movies: [String], expanded: Bool, subtitle: String){
    self.genre = genre
    self.movies = movies
    self.expanded = expanded
    self.subtitle = subtitle
}

I tried using #imageLiteral(resourceName) with some text afterwards like

" #imageLiteral(resourceName: "pos") + "a title" "

which is not syntactically correct.

How do I add an image before some text in a string?


Solution

  • Those are not image literals; they are ordinary characters (if you regard emoji as ordinary). Swift strings are unicode strings, so you are allowed to type an emoji character directly into a string literal.