Search code examples
swiftstringescaping

Adding quotes in String progammatically


I have run into an issue where I am trying to populate a global variable with values in a firebase table; the issue is I need the data to be delimited with quotes.

Here is the data as it stands right now:

var data1 = [
        quoteData(sectionType: "For You", quotes: ["Categories/General.png"]),
         
        quoteData(sectionType: "Free Today", quotes: [myFreeItems]),  
         
        quoteData(sectionType: "Inspiration", quotes: ["Categories/Feeling Sassy.png"])

]

As you will see, myFreeItems is a var that var data is pulled from a firebase table via the following code:

    override func viewDidLoad() {
    super.viewDidLoad()
    let myRef = myRef.reference().child("Quotes/Free Today")
    myRef.keepSynced(true)
    // observe value of reference
    myRef.observe(.value, with: { snapshot in
        for item in snapshot.children {
            let mItem = FreeItem(snapshot: item as! DataSnapshot)
            if mItem.act == "Y" {
                self.newItems.append(mItem)
            }
        }
        for index in self.newItems {
            if index.act == "Y" {
                var tempVal = "Categories/"
                if self.subCount <= self.newItems.endIndex - 1 {
                    print(self.subCount)
                    print(self.newItems.endIndex)
                    if self.subCount < self.newItems.endIndex - 1 {
                        tempVal += index.nm + ".png"
                        print(tempVal)
                        self.tmpVal += "\"" + tempVal + "\"" + ","
                        print(self.tmpVal)
                    } else {
                        tempVal += index.nm + ".png"
                        print(tempVal)
                        self.tmpVal += "\"" + tempVal + "\""
                        print(self.tmpVal)
                    }
                    self.subCount += 1
                }
            }
        }

        myFreeItems = self.dougie
        print(myFreeItems)
    })

Up to this point, everything is good. When I print out the statement I get the following:

"Categories/Death.png","Categories/Confidence.png","Categories/Sassy.png"

Exactly what I want.

Now the problem. I am referencing this var "myFreeItems" in another setup:

var data1 = [
        quoteData(sectionType: "For You", quotes: ["Categories/General.png"]),
         
        quoteData(sectionType: "Free Today", quotes: [myFreeItems])

]

When I print this out I get this:

Motivation.quoteData(sectionType: "Free Today", quotes: ["\"Categories/Death.png\",\"Categories/Confidence.png\",\"Categories/Sassy.png\""]),   

It is picking up the escape characters. What I need to see is the following:

quoteData(sectionType: "Free Today", quotes: ["Categories/Death.png", "Categories/Confidence.png", "Categories/Sassy.png"])

Any help would be greatly appreciated. I have also tried to use the enhanced hash mark # but that did not work either.


Solution

  • The problem is that you need myFreeItems to be declared as [String] instead of just String. And your logic in viewDidLoad should populate the array of strings instead of creating a single string with quotes and commas.

    Change var myFreeItems = "" to var myFreeItems = [String]()

    Then replace the for loop in viewDidLoad with this code:

    for index in self.newItems {
        if index.act == "Y" {
            if self.subCount <= self.newItems.endIndex - 1 {
                let tempVal = "Categories/" + index.nm + ".png"
                myFreeItems.append(tempVal)
                self.subCount += 1
            }
        }
    }
    

    Remove the line:

    myFreeItems = self.dougie
    

    Lastly, change:

    quoteData(sectionType: "Free Today", quotes: [myFreeItems])
    

    to:

    quoteData(sectionType: "Free Today", quotes: myFreeItems)