You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中Firebase数据添加引号后避免转义字符的问题

Swift Firebase数据填充数组的格式问题解决

问题场景

从Firebase数据表读取值填充全局变量,期望将数据作为字符串数组传入quoteData的quotes参数,但放入后出现转义字符,格式不符合预期。

当前代码与现象

Firebase读取逻辑

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)
    })

打印myFreeItems结果(符合预期)

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

传入quoteData的代码

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

]

错误打印结果(出现转义字符)

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

期望格式

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

解决方案

核心问题是你把拼接好的单个字符串当成了数组元素,而quotes需要的是字符串数组。不需要手动拼接引号和逗号,直接收集路径到数组即可:

  1. 修正全局变量类型:将myFreeItems定义为字符串数组,而非单个字符串
var myFreeItems: [String] = []
  1. 重构Firebase读取逻辑:直接收集符合条件的路径到数组,无需手动拼接格式
override func viewDidLoad() {
    super.viewDidLoad()
    // 修正原代码中重复引用的问题
    let myRef = Database.database().reference().child("Quotes/Free Today")
    myRef.keepSynced(true)
    
    myRef.observe(.value, with: { snapshot in
        var tempPaths: [String] = []
        for item in snapshot.children {
            let mItem = FreeItem(snapshot: item as! DataSnapshot)
            if mItem.act == "Y" {
                // 直接生成路径并添加到数组
                let itemPath = "Categories/\(mItem.nm).png"
                tempPaths.append(itemPath)
            }
        }
        // 更新全局数组
        self.myFreeItems = tempPaths
        
        // 异步更新data1(因为Firebase读取是异步的,初始化时myFreeItems为空)
        self.data1[1] = quoteData(sectionType: "Free Today", quotes: self.myFreeItems)
        
        print(self.myFreeItems)
        // 此时打印结果为:["Categories/Death.png", "Categories/Confidence.png", "Categories/Sassy.png"]
    })
}
  1. 修正data1初始化:由于Firebase读取是异步操作,初始化时myFreeItems为空,先给Free Today部分设空数组,后续在回调中更新
var data1 = [
    quoteData(sectionType: "For You", quotes: ["Categories/General.png"]),
    quoteData(sectionType: "Free Today", quotes: [])
]

原理说明

之前的代码手动将多个路径拼接成了一个带引号和逗号的长字符串,然后把这个字符串作为单个元素放进quotes数组,所以打印时会自动转义字符串内部的引号。正确的做法是让quotes直接接收字符串数组,每个路径都是数组的独立元素,这样就能得到你期望的格式。

内容的提问来源于stack exchange,提问作者Douglas W. Palme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:48:28