繁体   English   中英

迅捷2-意外地在访问字典数组时找到nil

[英]swift 2-unexpectedly found nil while accessing array of dictionary

我正在使用Swift 1进行在线课程。这些代码在1处还可以,但在2处显示运行时错误。

MusicLibrary.swift

let library = [
    [
        "title": "Rise and Shine",
        "description": "Get your morning going by singing along to these classic tracks as you hit the shower bright and early!",
        "icon": "coffee.pdf",
        "largeIcon": "coffee_large.pdf",
        "backgroundColor": ["red": 255, "green": 204, "blue": 51, "alpha": 1.0],
        "artists": ["American Authors", "Vacationer", "Matt and Kim", "MGMT", "Echosmith", "Tokyo Police Club", "La Femme"]
    ]

播放列表

    init(index: Int) {
    let musicLibrary = MusicLibrary().library
    let playlistDictionary = musicLibrary[index]

    title = playlistDictionary["title"] as String!
    description = playlistDictionary["description"] as String!

    let iconName = playlistDictionary["icon"] as String!
    icon = UIImage(named: iconName!)

    let largeIconName = playlistDictionary["largeIcon"] as String!
    largeIcon = UIImage(named: largeIconName!)

    artists += playlistDictionary["artist"] as [String]
}

我不知道为什么你会得到“艺术家”(字符串)而不是“艺术家”(数组)。 无论如何,使用您的代码,可以这样修复:

   if objects.objectForKey("artist") != nil
   {
      let artist = playlistDictionary("artist") as [String]
       artists += artist
   }

怎么了

在Swift 2中,您必须编写以下代码:

if let _title = playlistDictionary["title"] as? String{
   self.title = _title
}

或playlistDictionary是可选的:

if let _title = playlistDictionary?["title"] as? String{
   self.title = _title
}
let library:[Dictionary<String,Any>] = [
    [
        "title": "Rise and Shine",
        "description": "Get your morning going by singing along to these classic tracks as you hit the shower bright and early!",
        "icon": "coffee.pdf",
        "largeIcon": "coffee_large.pdf",
        "backgroundColor": ["red": 255, "green": 204, "blue": 51, "alpha": 1.0],
        "artists": ["_American Authors", "_Vacationer", "_Matt and Kim", "_MGMT", "_Echosmith", "_Tokyo Police Club", "_La Femme"]
    ],
    [
        "title": "Rise and Shine",
        "description": "Get your morning going by singing along to these classic tracks as you hit the shower bright and early!",
        "icon": "coffee.pdf",
        "largeIcon": "coffee_large.pdf",
        "backgroundColor": ["red": 255, "green": 204, "blue": 51, "alpha": 1.0],
        "artists": ["American Authors", "Vacationer", "Matt and Kim", "MGMT", "Echosmith", "Tokyo Police Club", "La Femme"]
    ]
]


var artists: [String] = []
library.forEach { (dict) -> () in
    if let arr = dict["artists"] as? [String] {
        artists += arr
    }
}
print(artists)
/*
["_American Authors", "_Vacationer", "_Matt and Kim", "_MGMT", "_Echosmith", "_Tokyo Police Club", "_La Femme", "American Authors", "Vacationer", "Matt and Kim", "MGMT", "Echosmith", "Tokyo Police Club", "La Femme"]

*/

如果您的图书馆是[NSDictionary],它的运作方式仍然相同

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM