gpt4 book ai didi

swift - 将 if 子句转换为可选值

转载 作者:行者123 更新时间:2023-11-30 10:15:59 25 4
gpt4 key购买 nike

我正在尝试将大量 if 子句转换为 Swift 中的可选值。我仍然收到 let name 语句的错误。我收到的错误是:

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

我有两个问题:

  1. 我的新代码与旧代码有相同的用途吗?
  2. 如何消除此错误?

谢谢。

Old Code:

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
}

New code:

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
}

感谢大家的大力支持,我们才得以实现这一目标。在这个新版本中,我收到错误“使用未解析的标识符‘名称’”。此错误发生在流量控制 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
}

最佳答案

已经解决了。该代码的工作原理如下。我进行了错误的深入研究。真的谢谢你,我学到了很多东西:

         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
}

关于swift - 将 if 子句转换为可选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29926716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com