prank/Funny_sounds/Home/M/prankModel.swift
2024-09-03 09:38:34 +08:00

65 lines
1.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Prankmodel.swift
// Funny_sounds
//
// Created by 16 on 2024/8/15.
//
import Foundation
//
class AudioFile:NSObject, Codable {
let title: String
let mp3Url: String
let preUrl: String
//
init(title: String, mp3Url: String, preUrl: String) {
self.title = title
self.mp3Url = mp3Url
self.preUrl = preUrl
}
// Codable
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.title = try container.decode(String.self, forKey: .title)
self.mp3Url = try container.decode(String.self, forKey: .mp3Url)
self.preUrl = try container.decode(String.self, forKey: .preUrl)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(title, forKey: .title)
try container.encode(mp3Url, forKey: .mp3Url)
try container.encode(preUrl, forKey: .preUrl)
}
private enum CodingKeys: String, CodingKey {
case title
case mp3Url
case preUrl
}
}
//
class SoundCategory:NSObject,Codable {
let categoryId: String
let categoryName: String
let categoryUrl: String
let list: [AudioFile]
}
func parseJSONData(jsonData: Data) -> [SoundCategory]? {
let decoder = JSONDecoder()
do {
//
let soundCategories = try decoder.decode([SoundCategory].self, from: jsonData)
return soundCategories
} catch {
print("解析JSON数据时出错: \(error)")
return nil
}
}