Ios Swift-使用'时为零;作为!字典<;字符串,任意对象>';

Ios Swift-使用'时为零;作为!字典<;字符串,任意对象>';,ios,arrays,swift,Ios,Arrays,Swift,Swift noobie在这里尝试按照教程构建自定义贴纸应用程序。我可以将应用程序编译到模拟器,但不能编译到手机 初始代码如下: class FoodDrawerCollectionViewController: UICollectionViewController { let numberOfItemsPerRow = 3.0 as CGFloat let interItemSpacing = 1.0 as CGFloat let interRowSpacing = 1.0 as CGFloa

Swift noobie在这里尝试按照教程构建自定义贴纸应用程序。我可以将应用程序编译到模拟器,但不能编译到手机

初始代码如下:

class FoodDrawerCollectionViewController: UICollectionViewController {
let numberOfItemsPerRow = 3.0 as CGFloat
let interItemSpacing = 1.0 as CGFloat
let interRowSpacing = 1.0 as CGFloat
let sectionTitleKey = "SectionTitle"
let sectionItemsKey = "Items"
var data = [Dictionary<String,AnyObject>]()
override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView?.contentInset = UIEdgeInsets(top: 80, left: 0, bottom: 40, right: 0)
    if let path = Bundle.main.path(forResource: "FoodDrawerData", ofType: ".plist") {
        let dict = NSDictionary(contentsOfFile: path) as! Dictionary<String,AnyObject>
        let allSections = dict["Sections"]
        if let selectedSections = UserDefaults.standard.array(forKey: "selectedSections") as? [Int] {
            for index in selectedSections {
                    self.data.append((allSections![index.description]) as! Dictionary<String, AnyObject>)
            }
        }
    }
}
class FoodDrawerCollectionViewController:UICollectionViewController{
将numberOfItemsPerRow=3.0设为CGFloat
让interItemSpacing=1.0作为CGFloat
设interRowSpacing=1.0作为CGFloat
让sectionTitleKey=“SectionTitle”
让sectionItemsKey=“Items”
变量数据=[字典]()
重写func viewDidLoad(){
super.viewDidLoad()
self.collectionView?.contentInset=uiedgeInSet(顶部:80,左侧:0,底部:40,右侧:0)
如果let path=Bundle.main.path(forResource:“FoodDrawerData”,类型:“.plist”){
将dict=NSDictionary(contentsOfFile:path)设为!Dictionary
让allSections=dict[“Sections”]
如果让selectedSections=UserDefaults.standard.array(forKey:“selectedSections”)作为?[Int]{
用于选定节中的索引{
self.data.append((allSections![index.description])as!Dictionary)
}
}
}
}
引起我问题的线路是

 self.data.append((allSections![index.description]) as! Dictionary<String, AnyObject>)
self.data.append((allSections![index.description])as!Dictionary)
我不知道如何修改代码以防止打开可选值,因为我看不到有助于我重新编码这部分代码的直接示例。是否有人可以建议并解释我的变量发生了什么,以及它是否是
索引。说明
数据
选定部分
,或som还有什么原因导致这个问题


谢谢你们!你们走对了

    // use as? instead of as!
    // it might return nil
    let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String,AnyObject>
    if let dict = dict {
        // the following might be nil
        if let allSections = dict["Sections"] {
            // Sections was found
        }
        else {
            // Sections not found
        }
    }
    else {
        // dict is nil
    }
//使用as?而不是as!
//它可能返回零
让dict=NSDictionary(contentsOfFile:path)作为字典
如果让dict=dict{
//以下可能是零
如果让allSections=dict[“Sections”]{
//找到了一些切片
}
否则{
//找不到节
}
}
否则{
//dict为零
}

您走在正确的轨道上

    // use as? instead of as!
    // it might return nil
    let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String,AnyObject>
    if let dict = dict {
        // the following might be nil
        if let allSections = dict["Sections"] {
            // Sections was found
        }
        else {
            // Sections not found
        }
    }
    else {
        // dict is nil
    }
//使用as?而不是as!
//它可能返回零
让dict=NSDictionary(contentsOfFile:path)作为字典
如果让dict=dict{
//以下可能是零
如果让allSections=dict[“Sections”]{
//找到了一些切片
}
否则{
//找不到节
}
}
否则{
//dict为零
}

尝试使用[String:AnyObject]这是一个非常糟糕的教程。它使用,而不是:和大量的强制展开动作。尽可能远离
,并始终使用
if let
guard let
as?
等检查您的选项是否包含值。尝试使用[String:AnyObject]这是一个相当糟糕的教程。它使用了,而不是:和大量的强制展开动作。尽可能远离
,并且始终检查您的选项是否包含一个带有
if let
guard let
as?
等的值。从应用程序包检索的数据的可选绑定是毫无意义的。您需要ted文件,它不能是
nil
,你知道它的内容类型。准确地说。如果它是nil,那么你希望崩溃并修复文件,而不是处理错误。这是你的意见。在你的代码中处理它,不管你想怎样。我宁愿优雅地处理任何异常情况。这不是一个意见,这是一个事实。这不仅仅是黑与黑处理选项时为白色。设计(时间)之间存在显著差异错误–可控制–运行时错误–不可控制。从应用程序包检索的数据的可选绑定毫无意义。您创建了文件,它不可能是
nil
,并且您知道其内容的类型。准确地说。如果是nil,则您希望崩溃并修复文件,而不是处理错误。这是您的意见。Han不管你怎么想,都可以在你的代码中使用它。我宁愿优雅地处理任何异常情况。这不是观点,这是事实。在处理选项时,存在的不仅仅是黑白。设计(时间)错误(可控)和运行时错误(不可控)之间有着显著的区别。