将if子句转换为可选项

时间:2015-04-28 18:02:39

标签: swift

我试图将很多if子句转换为Swift中的选项。我仍然收到let name声明的错误。我得到的错误是:

Type 'int' does not conform to protocol 'StringLiteralConvertible'.

我有两个问题:

  1. 我的新代码是否与旧代码具有相同的用途?
  2. 我怎样才能摆脱这个错误?
  3. 感谢。

      

    旧代码:

    class func jsonAsUSDAIdAndNameSearchResults (json: NSDictionary) -> [(name: String, idValue: String)] {
    
        var usdaItemsSearchResults: [(name: String, idValue: String)] = []
        var searchResult: (name: String, idValue: String)
    
        if json["hits"] != nil {
            let results: [AnyObject] = json["hits"] as [AnyObject]
            for itemDictionary in results {
                if itemDictionary["_id"] != nil {
                    let idValue:String = itemDictionary["_id"] as String
                    if itemDictionary["fields"] != nil {
                        let fieldsDictionary: NSDictionary = itemDictionary["fields"] as NSDictionary
                        if fieldsDictionary["item_name"] != nil {
                            let name: String = fieldsDictionary["item_name"] as String
                            searchResult = (name: name, idValue: idValue)
                            usdaItemsSearchResults += [searchResult]
                        }
                    }
                }
            }
        }
        return usdaItemsSearchResults
    }
    
      

    新代码:

    class func jsonAsUSDAIdAndNameSearchResults (json: NSDictionary) -> [(name: String, idValue: String)] {
    
        var usdaItemsSearchResults: [(name: String, idValue: String)] = []
        var searchResult: (name: String, idValue: String)
    
        if json["hits"] != nil {
            let results: [AnyObject] = json["hits"] as [AnyObject]
            for itemDictionary in results {
                let name: String? = itemDictionary["_id"]?["_fields"]?["item_name"]? as? String
                let idValue:String? = itemDictionary["_id"]? as? String
                if (name? != nil && idValue? != nil) {
                    searchResult = (name: name!, idValue: idValue!)
                    usdaItemsSearchResults += [searchResult]
                }
            }
        }
        return usdaItemsSearchResults
    }
    

    由于出色的支持,我们到了那里。在这个新版本中,我收到错误"使用未解析的标识符' name'"。此错误发生在流量控制if (name? != nil ...)的级别。

    这是新代码:

            class func jsonAsUSDAIdAndNameSearchResults (json: NSDictionary) -> [(name: String, idValue: String)] {
    
        var usdaItemsSearchResults: [(name: String, idValue: String)] = []
        var searchResult: (name: String, idValue: String)
    
        if json["hits"] != nil {
            let results:[AnyObject] = json["hits"]! as [AnyObject]
            for itemDictionary in results {
                if let name = ((itemDictionary["_id"] as! NSDictionary)["_fields"] as! NSDictionary)["item_name"] as? String {break}
                if let idValue = itemDictionary["_id"] as? String {break}
                if (name? != nil && idValue? != nil) {
                    searchResult = (name: name!, idValue: idValue!)
                    usdaItemsSearchResults += [searchResult]
                }
            }
        }
        return usdaItemsSearchResults
    }
    

    Grimxn值得一枚奖牌!我们就快到了。以下代码不再显示任何问题,但我们在运行它时遇到了断点。它似乎与铸造有问题。

    class func jsonAsUSDAIdAndNameSearchResults (json: NSDictionary) -> [(name: String, idValue: String)] {
    
            var usdaItemsSearchResults: [(name: String, idValue: String)] = []
            var searchResult: (name: String, idValue: String)
    
            if json["hits"] != nil {
                let results:[AnyObject] = json["hits"]! as [AnyObject]
                for itemDictionary in results {
                    let fields: NSDictionary = (itemDictionary["_id"]? as NSDictionary)["_fields"]? as NSDictionary
                    let name:String? = fields["item_name"] as? String
                    let idValue:String? = itemDictionary["_id"]? as? String
                    if (name? != nil && idValue? != nil) {
                        searchResult = (name: name!, idValue: idValue!)
                        usdaItemsSearchResults += [searchResult]
                    }
                }
            }
            return usdaItemsSearchResults
        }
    

    它解决了。代码的工作原理如下。我做错了。真的,谢谢,我学到了很多东西:

             class func jsonAsUSDAIdAndNameSearchResults (json: NSDictionary) -> [(name: String, idValue: String)] {
    
        var usdaItemsSearchResults: [(name: String, idValue: String)] = []
        var searchResult: (name: String, idValue: String)
    
        if json["hits"] != nil {
            let results:[AnyObject] = json["hits"]! as [AnyObject]
            for itemDictionary in results {
                let name:String? = (itemDictionary["fields"]? as NSDictionary)["item_name"] as? String
                let idValue:String? = itemDictionary["_id"]? as? String
                if (name? != nil && idValue? != nil) {
                    searchResult = (name: name!, idValue: idValue!)
                    usdaItemsSearchResults += [searchResult]
                }
            }
        }
        return usdaItemsSearchResults
    }
    

2 个答案:

答案 0 :(得分:1)

它解决了。代码的工作原理如下。我做错了。真的,谢谢,我学到了很多东西:

         class func jsonAsUSDAIdAndNameSearchResults (json: NSDictionary) -> [(name: String, idValue: String)] {

    var usdaItemsSearchResults: [(name: String, idValue: String)] = []
    var searchResult: (name: String, idValue: String)

    if json["hits"] != nil {
        let results:[AnyObject] = json["hits"]! as [AnyObject]
        for itemDictionary in results {

            let fields: NSDictionary = itemDictionary["fields"]? as NSDictionary
            let name:String? = fields["item_name"] as? String
            let idValue:String? = itemDictionary["_id"]? as? String
            if (name? != nil && idValue? != nil) {
                searchResult = (name: name!, idValue: idValue!)
                usdaItemsSearchResults += [searchResult]
            }
        }
    }
    return usdaItemsSearchResults
}

答案 1 :(得分:0)

错误在let name: String? = itemDictionary["_id"]?["_fields"]?["item_name"]? as? String 行 根据您的旧代码,您可以直接从itemDictionary获取字段对象。

func jsonAsUSDAIdAndNameSearchResults (json: NSDictionary) -> [(name: String, idValue: String)] {

var usdaItemsSearchResults: [(name: String, idValue: String)] = []
var searchResult: (name: String, idValue: String)

if json["hits"] != nil {
    let results: [AnyObject] = json["hits"] as [AnyObject]
    for itemDictionary in results {
        let name: String? = itemDictionary["_fields"]?["item_name"]? as? String
        let idValue:String? = itemDictionary["_id"]? as? String
        if (name? != nil && idValue? != nil) {
            searchResult = (name: name!, idValue: idValue!)
            usdaItemsSearchResults += [searchResult]
        }
    }
}
return usdaItemsSearchResults

} 由于您在if循环中检查nil值,它应该可以工作。