我正在努力實作以下目標,需要您在處理swift中的字典方面提供幫助:
我正在努力實作以下目標,需要您在處理swift中的字典方面提供幫助。
- 如何獲得 "Nobody "的 "影像 "值?
- 如何獲得 "Nobody "的 "Image "值? 它應該是 "Nobody.png"
- 如何計算BestMoments成員的數量?
- 如何計算BestMoments成員的數量?(第一部電影應該是3,第二部電影應該是4"
- 如何獲得BestMoments成員的值?
- 如何獲得 "Nobody "的Bestmoments內的值(如:"Survive",120,8)?
var MV = [
["Nobody": [
["YEAR" : "2021"] ,
["IMAGE" : "Nobody.png"] . ,
["GENRE" : "Action/Thriller"] . ,
["DURATIONhours" : 1] . ,
["DURATIONmintues" : 36] ,
["DURATIONseconds" : 17] ,
["BestMoments" 。[["Win", 25, 5], ["Survive", 120, 8], ["Lose", 240, 15]]
]],
["哥斯拉與金剛": ["哥斯拉與金剛": [
["YEAR" : "2021"] 。,
["IMAGE" : "Nobody.png"] . ,
["GENRE" : "Action/Sci-fi"] . ,
["DURATIONhours" : 1] . ,
["DURATIONmintues" : 22] ,
["DURATIONseconds" : 45] . ,
["BestMoments" 。[["Win", 34, 6] 。["Survive", 120, 8] 。["Twist", 233, 3] 。["Lose", 340, 12] ]]
]],
]
我試著用下面的方法來訪問資料(有結果):
//元組模式不能匹配非元組型別的值 [String : [[String : Any]]] uj5u.com熱心網友回復: 有關的資料有一個非常復雜的資料結構,很難管理。我認為有一個更簡單的結構會更好。 然而,你可以通過以下方式訪問你想要的資料。
試著列印一下結果。 uj5u.com熱心網友回復: 更好地表述資料: 那么你的操作就很簡單了: 你甚至可以把你的電影庫包裝成一個額外的物件,這將簡化電影的搜索作業。
時間可能應該只用秒來表示,只有在需要時才轉換為小時/分鐘/秒。
標籤: 下一篇:Python設定動態值到字典
print(MV)
print(MV[0])
//列印第一部電影的所有資料print(MV[0])
print(MV["Nobody"])
//在呼叫下標時沒有精確的匹配print([MV["Nobody"])
//列印第一部電影的所有資料。
print([MV[0].keys]) //code>
for (movie, data) in MV2{
print(data)
//1.如何獲得 "Nobody "的 "Image "值? 它應該是 "Nobody.png"
let imageOfNobady = MV[0]["Nobody" ]! [1]["IMAGE"]
//2. 如何計算BestMoments成員的數量?(第一部電影應該是3個,第二部電影應該是4個")
let bestmomentsForNobady = MV[0]["Nobody"/span>]! [6]["BestMoments"] as! [Any]
let countOfBestmomentsForNobady = bestmomentsForNobady.count
//3.如何在Bestmoments里面獲取 "Nobody "的值(如: "Survive", 120, 8)?
let survieInfo = bestmomentsForNobady[1]
struct BestMoment {
enum MomentType: String {
case win = "Win"
case survive = "Survive"
case lose = "los"
case twist = "扭曲"
}
let type: MomentType
let start: Int
let duration: Int
}
struct Duration {
let hours: Int
let minutes: Int
let seconds: Int
}
struct Movie {
let name: String String
let year: Int
let image: String
let genre: String[/span]>
let duration: Duration Let
let bestMoments: [BestMoment] 。
}
let movies = [
Movie(
名稱。"Nobody",
年。2021,
影像。"Nobody.png",
型別。"動作/驚悚"。
持續時間。Duration(小時。1, 分鐘。36, seconds: 16)。)
bestMoments。[
BestMoment(type: .win, start: 25, duration: 5)。)
BestMoment(type: .survive, start: 120, duration: 8)。)
BestMoment(type: .los, start: 240, duration: 15)
]
),
Movie(
名稱。"Godzilla vs. Kong"。
年。2021,
影像。"Nobody.png",
型別。"Action/Sci-fi",
持續時間。Duration(小時。1, minutes: 22, seconds: 45)。)
bestMoments。[
BestMoment(type: .win, start: 34, duration: 6)。)
BestMoment(type: .survive, start: 120, duration: 8)。)
BestMoment(type: .twist, start: 233, duration: 3)。)
BestMoment(type: .los, start: 340, duration: 12)
]
)
]
movies.first { $0.name == "Nobody" }?.imagemovies.first { $0.name == "Nobody" }?.bestMoments.countmovies.first { $0.name == "Nobody" }?.bestMomentsGenre可能是一個enum。
