简体   繁体   English

检查字典中的对象是否为Int(Swift)

[英]Check if an Object in a Dictionary is an Int (Swift)

I'm relatively new to coding iOS and have not fully wrapped my head around optionals, downcasting, dictionaries and related fun concepts. 我对编写iOS编程还比较陌生,还没有完全了解可选参数,向下转换,字典和相关的有趣概念。 I would greatly appreciate help on the following. 在以下方面,我将不胜感激。

I am downloading data from a database and want to perform checks on the data to avoid a crash. 我正在从数据库下载数据,并希望对数据进行检查以避免崩溃。 In this particular case I want to check if an Object in a dictionary is an Int before performing a task to avoid a crash. 在这种特殊情况下,我想在执行任务以避免崩溃之前检查字典中的对象是否为Int。

//The downloaded dictionary includes Int, Double and String data
var dictionaryDownloaded:[NSDictionary] = [NSDictionary]()

//Other code for downloading the data into the dictionary not shown.

for index in 1...dictionaryDownloaded.count {

    let jsonDictionary:NSDictionary = self.dictionaryDownloaded[index]

    if (jsonDictionary["SUNDAY OPEN TIME"] as? [Int]) != nil {
        self.currentlyConstructingRecommendation.sundayOpenTime = jsonDictionary["SUNDAY OPEN TIME"] as! Int!
    }

    self.recommendationsArray.append(currentlyConstructingRecommendation)
}

I followed the approach from this related question and answer . 我遵循了相关问答中的方法。 However, the problem is the "if (jsonDictionary["SUNDAY OPEN TIME"] as? [Int]) != nil" line is never true. 但是,问题是“如果(jsonDictionary [“ SUNDAY OPEN TIME”] as?[Int])!= nil”这一行从不成立。 I believe that is because the value is an optional object. 我相信这是因为该值是一个可选对象。 I tried adjusting the dictionary to be of type [String:AnyObject] but that did not have an impact. 我尝试将字典调整为[String:AnyObject]类型,但这没有影响。

I'm stuck and any ideas you have would be appreciated. 我被困住了,您的任何想法都将不胜感激。 Please let me know if there is any more detail that is helpful. 请让我知道是否有更多有用的细节。 Thanks! 谢谢!

With this code: jsonDictionary["SUNDAY OPEN TIME"] as? [Int] 使用以下代码: jsonDictionary["SUNDAY OPEN TIME"] as? [Int] jsonDictionary["SUNDAY OPEN TIME"] as? [Int] , you are trying to convert the value to Array<Int> , not an Int . jsonDictionary["SUNDAY OPEN TIME"] as? [Int] ,您正在尝试将值转换为Array<Int> ,而不是Int

And in your code, you have another flaw: index in 1...dictionaryDownloaded.count . 并且在代码中,您还有另一个缺陷: index in 1...dictionaryDownloaded.count This causes Index out of range exception, when index gets to dictionaryDownloaded.count . index到达dictionaryDownloaded.count时,这将导致索引超出范围异常。

So, a quick fix would be: 因此,一个快速的解决方法是:

for index in 0..<dictionaryDownloaded.count {

    let jsonDictionary:NSDictionary = self.dictionaryDownloaded[index]

    if (jsonDictionary["SUNDAY OPEN TIME"] as? Int) != nil {
        self.currentlyConstructingRecommendation.sundayOpenTime = jsonDictionary["SUNDAY OPEN TIME"] as! Int!
    }

    self.recommendationsArray.append(currentlyConstructingRecommendation)
}

But I recommend you to do it in a more Swifty way. 但我建议您以一种更加快捷的方式进行操作。

for jsonDictionary in dictionaryDownloaded {

    if let sundayOpenTime = jsonDictionary["SUNDAY OPEN TIME"] as? Int {
        self.currentlyConstructingRecommendation.sundayOpenTime = sundayOpenTime
    }

    self.recommendationsArray.append(currentlyConstructingRecommendation)
}

I think you've confused Int (which is an integer) with [Int] (which is an array of integer s ). 我认为您已经将Int (这是一个整数)与[Int] (这是一个整数s数组 )混淆了。 Furthermore, this section of your code is redundant: 此外,这段代码是多余的:

if (jsonDictionary["SUNDAY OPEN TIME"] as? [Int]) != nil {
    self.currentlyConstructingRecommendation.sundayOpenTime = jsonDictionary["SUNDAY OPEN TIME"] as! Int!
}

You use the excellent as? 您用的出色as? operator to perform a conditional cast, but then you discard the result and use the dangerous as! 操作符执行条件转换,但随后您将结果丢弃,并将危险as! on the next line. 在下一行。 You can use an if let to make this safer and clearer: 您可以使用if let更安全,更清晰:

if let sundayOpenTime = jsonDictionary["SUNDAY OPEN TIME] as? Int {
    self.currentlyConstructingRecommendation.sundayOpenTime = sundayOpenTime
}

This casts the type to Int , and if that result is not nil , unwraps it and sets sundayOpenTime to it. 这将类型强制转换为Int ,如果结果不是nil ,则将其解包并sundayOpenTime设置sundayOpenTime We then use this new sundayOpenTime constant of type Int in the next line. 然后,在下一行中使用Int类型的这个新的sundayOpenTime常量。 If, however, the result of the cast is nil , the entire if statement fails and we move on safely. 但是,如果强制转换的结果 nil ,则整个if语句将失败,我们将继续进行。

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

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