gpt4 book ai didi

ios - Swift-如何将复杂对象写入Firebase实时数据库

转载 作者:行者123 更新时间:2023-12-01 21:51:56 24 4
gpt4 key购买 nike

我是Firebase实时数据库的新手,而Swift则相对较新。我正在尝试构建一个歌曲请求应用程序,其中用户可以为访客创建事件以从Spotify API请求歌曲。我正在尝试将Event对象写入Firebase,其中包含嵌套对象和不同类型的数组。但是,当它写入数据库时​​,它仅写入字符串,而不写入数组或对象。将所有这些信息以嵌套结构的形式写入Firebase数据库的最佳方法是什么,以便每当用户添加歌曲请求时,我都可以在firebase中编辑针对给定事件的请求数组。
这是我的代码:
Event.swift

struct Event: Codable{

var code: String
var name: String
var host: String
var description: String
var hostUserId: String
var guestIds: [String]
var requests: [Request]
var queue: [Request]
var played: [Request]
//private var allowExplicit: Bool
//private var eventLocation

init(code: String, name: String, host: String, description: String, hostUserId: String){
self.code = code
self.name = name
self.host = host
self.description = description
self.hostUserId = hostUserId
self.guestIds = []
self.requests = []
self.queue = []
self.played = []
}

func toAnyObject() -> Any{
var guestIdsDict: [String:String] = [:]
for id in guestIds{
guestIdsDict[id] = id
}

var requestsDict: [String: Any] = [:]
for request in requests{
requestsDict[request.getId()] = request.toAnyObject()
}

var queueDict: [String: Any] = [:]
for request in queue{
queueDict[request.getId()] = request.toAnyObject()
}

var playedDict: [String: Any] = [:]
for request in played{
playedDict[request.getId()] = request.toAnyObject()
}

return [
"code": code,
"name": name,
"host": host,
"description": description,
"hostUserId": hostUserId,
"guestIds": guestIdsDict,
"requests": requestsDict,
"queue":queueDict,
"played":playedDict
]
}
}
Request.swift
struct Request: Codable{
private var name: String
private var id: String
private var explicit: Bool
private var album: Album
private var artists: [Artist]
private var likes: Int

init(name: String, id: String, explicit: Bool, album: Album, artists: [Artist]){
self.name = name
self.id = id
self.explicit = explicit
self.album = album
self.artists = artists
self.likes = 1
}

func toAnyObject() -> Any{
var artistsDict: [String:Any] = [:]
for artist in artists {
artistsDict[artist.id] = artist.toAnyObject()
}
return [
"name": name,
"id": id,
"explicit": explicit,
"album": album.toAnyObject(),
"artists": artistsDict,
"likes": likes
]
}

mutating func like(){
self.likes += 1
}

mutating func unlike(){
self.likes -= 1
if(self.likes < 0){
self.likes = 0
}
}

mutating func setLikes(count: Int){
self.likes = count
}

func getLikes() -> Int{
return self.likes
}

func getName() -> String{
return self.name
}

func getId() -> String{
return self.id
}

func getExplicit() -> Bool{
return self.explicit
}

func getAlbum() -> Album {
return self.album
}

func getImages() -> [Image] {
return self.album.images
}

func getArtists() -> [Artist] {
return self.artists
}

func getArtistString() -> String{
var artistString = ""
for (i, artist) in self.artists.enumerated(){
artistString += artist.name
if(i != self.artists.endIndex-1){
artistString += ", "
}
}
return artistString
}
}
相册快速
struct Album: Codable{
let name: String
let images: [Image]

func toAnyObject() -> Any{
var imagesDict: [String: Any] = [:]
for image in images{
imagesDict[image.url] = image.toAnyObject()
}
return [
"name": name,
"images": imagesDict
]
}
}
Artist.swift
struct Artist: Codable{
let id: String
let name: String

func toAnyObject() -> Any{
return ["id": id, "name": name]
}
}
图像扫描
struct Image: Codable{
let height: Int
let url: String
let width: Int

func toAnyObject() -> Any{
return ["height": height, "url": url, "width": width]
}
}

最佳答案

当您使用Codable时,可以如下创建一个dic:
步骤1:将此扩展名添加到您的代码中

extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
}
}
第2步:在Struct中编写以下代码(您必须在每个结构中执行此操作,也可以根据需要修改代码)。
func createDic() -> [String: Any]? {
guard let dic = self.dictionary else {
return nil
}

return dic
}
现在,在struct obj的帮助下,调用createDic()方法,您将获得一个字典。
您可以将此字典发送到firebase。
完整代码示例:
extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
}

struct LoginModel: Codable {
let email: String
let password: String

func createDic() -> [String: Any]? {
guard let dic = self.dictionary else {
return nil
}

return dic
}
}
如有任何疑问,请发表评论。
乐于帮助!

关于ios - Swift-如何将复杂对象写入Firebase实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61536726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com