如何将attributeedstring保存到Parsse服务器对象类型字段。

问题描述 投票:0回答:1

我试图将一个属性文本保存到Parsse服务器中,字段类型是Object。

请看下面的代码


            let htmlData = try attributedText
                .data(from: NSRange(location: 0,
                                    length: attributedText.length),
                      documentAttributes: documentAttributes)
            // htmlData is Data type
            let note = PFObject(className:"Note")
            note["Data"] = htmlData
            note.saveEventually { (success, error) in
                if (success) {
                    // success is false
                }
            }

我得到了这个错误

Note.Data的模式不匹配;预期是Object,但得到了Bytes。

注: 注:数据 列型 对象

有什么办法可以解决这个问题吗?

谅谅

ios swift parse-platform swift5.2
1个回答
1
投票

htmlData 是您的属性字符串的二进制数据表示。二进制数据不能直接存储在数据库中,因为 Parse Server 不支持类型为 BLOB. 你需要一个与 Parse Server 的字段类型相兼容的二进制数据表示,如 String 类型。

您可以将二进制数据转换为 基数64 编码 String 是一种ASCII表示法,简单地说,就是可读文本。

// Create binary data from attributed text
let htmlData = try attributedText.data(
    from: NSRange(location: 0, length: attributedText.length),
    documentAttributes: documentAttributes)

// Create string from binary data
let base64HtmlData = htmlData.base64EncodedData(options:[])

// Store string in Parse Object field of type `String`
let note = PFObject(className: "Note")
note["Data"] = base64HtmlData

你必须在 "解析仪表板 "中确定你的类型是 Data 栏目是 String.

然而,请记住,大型数据应该存储在解析文件中,因为解析对象的大小被限制为 128 KB. 另外,你不希望在MongoDB数据库中出现大数据BLOB,因为当你扩展时,它会对性能产生负面影响。

© www.soinside.com 2019 - 2024. All rights reserved.