如何将 txt 文件中的数据解析为模型?

How parse data from txt file into a Model?

我有一个包含数据的 txt 文件,我需要对其进行解析:

Dance
#dance #music #dancer #love #viral #hiphop #like #instagood #fashion #tiktok #instagram #follow #ballet #rap #explore #trending #explorepage #likeforlikes #art #dancelife #followme #video #youtube #dancechallenge #d #cute #pocodance

Funny
#funny #lol #lmao #lmfao #hilarious #laugh #laughing #tweegram #fun #friends #photooftheday #friend #wacky #crazy #silly #witty #instahappy #joke #jokes #joking #epic #instagood #instafun #funnypictures #haha #humor

Gaming
#gamergirl #gamestagram #video #winning #gaminglife #pcgaming #gamer #online #playing #playinggames #videogames #instagaming #gamerguy #instagamer #onlinegaming #game #play #gamingsetup #instagood #videogameaddict #photooftheday #games #gaming #pc #gamin #instagame #gamingmemes

我要解析成模型:

struct HashTags {
    var title = ""
    var tags = [String]()
}

我尝试使用下一段代码将txt文件转换成我需要的数据:

class ViewController: UIViewController {
    
    var hashtags = [HashTags]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        convert()
        print(hashtags)
    }


    func convert() {
        do {
            if let path = Bundle.main.path(forResource: "tags", ofType: "txt"){
                let data = try String(contentsOfFile: path, encoding: .utf8)
                let tuples = data.split(whereSeparator: \.isNewline)
                
                var title = ""
                var tags = [String]()
                
                for (position, item) in tuples.enumerated() {
                    if position % 2 == 0 {
                        title = String(item)
                    } else {
                        tags = item.components(separatedBy: " ")
                    }
                    
                    let tag = HashTags(title: title, tags: tags)
                    hashtags.append(tag)
                }
                
            }
        } catch let err as NSError {
            print(err)
        }
        
    }

}

但是我得到下一个重复的数据:

[TikTags.HashTags(title: "Dance", tags: []),
TikTags.HashTags(title: "Dance", tags: ["#dance", "#music", "#dancer", "#love", "#viral", "#hiphop", "#like", "#instagood", "#fashion", "#tiktok", "#instagram", "#follow", "#ballet", "#rap", "#explore", "#trending", "#explorepage", "#likeforlikes", "#art", "#dancelife", "#followme", "#video", "#youtube", "#dancechallenge", "#d", "#cute", "#pocodance"]),
TikTags.HashTags(title: "Funny", tags: ["#dance", "#music", "#dancer", "#love", "#viral", "#hiphop", "#like", "#instagood", "#fashion", "#tiktok", "#instagram", "#follow", "#ballet", "#rap", "#explore", "#trending", "#explorepage", "#likeforlikes", "#art", "#dancelife", "#followme", "#video", "#youtube", "#dancechallenge", "#d", "#cute", "#pocodance"]),
TikTags.HashTags(title: "Funny", tags: ["#funny", "#lol", "#lmao", "#lmfao", "#hilarious", "#laugh", "#laughing", "#tweegram", "#fun", "#friends", "#photooftheday", "#friend", "#wacky", "#crazy", "#silly", "#witty", "#instahappy", "#joke", "#jokes", "#joking", "#epic", "#instagood", "#instafun", "#funnypictures", "#haha", "#humor"]),
TikTags.HashTags(title: "Gaming", tags: ["#funny", "#lol", "#lmao", "#lmfao", "#hilarious", "#laugh", "#laughing", "#tweegram", "#fun", "#friends", "#photooftheday", "#friend", "#wacky", "#crazy", "#silly", "#witty", "#instahappy", "#joke", "#jokes", "#joking", "#epic", "#instagood", "#instafun", "#funnypictures", "#haha", "#humor"]),
TikTags.HashTags(title: "Gaming", tags: ["#gamergirl", "#gamestagram", "#video", "#winning", "#gaminglife", "#pcgaming", "#gamer", "#online", "#playing", "#playinggames", "#videogames", "#instagaming", "#gamerguy", "#instagamer", "#onlinegaming", "#game", "#play", "#gamingsetup", "#instagood", "#videogameaddict", "#photooftheday", "#games", "#gaming", "#pc", "#gamin", "#instagame", "#gamingmemes"])]

我做错了什么?我怎样才能得到我想要的?

问题是您在每个循环中迭代 1 行,其中标题行没有标签,而不同标签有以前的标题,您需要

for (position, item) in tuples.enumerated() {
    if position % 2 == 0 {
        title = String(item)
    } else {
        tags = item.components(separatedBy: " ")
        let tag = HashTags(title: title, tags: tags)
        hashtags.append(tag)
    }
}