该应用程序在检查NSMutablearray的计数是否为零时崩溃,我没有得到如何处理,我是ios的新手
let userDefaults: UserDefaults? = UserDefaults.standard
let array = userDefaults?.object(forKey: "purchaselist") as? NSMutableArray
for i in 0..<array!.count {
}
答案 0 :(得分:2)
您还应该检查nil,
if let array = userDefaults?.object(forKey: "purchaselist") as? [Any], !array.isEmpty {
//Your code goes here
}
您也可以这样做,
if let array = userDefaults?.object(forKey: "purchaselist") as? NSMutableArray {
if array.count != 0 {
//Your code goes here
} else {
//array count zero
}
} else {
//Your array is nil
}
仅供参考。代码未经测试,仅是信息。