我試圖在Swift中將很多if子句轉換爲可選項。我仍然收到let name
聲明的錯誤。我得到的錯誤是:將if子句轉換爲可選項
Type 'int' does not conform to protocol 'StringLiteralConvertible'.
我有兩個問題:
- 請問我的新代碼有我的舊代碼相同的使用?
- 我該如何擺脫這個錯誤?
謝謝。
舊代碼:
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
}
我們到達那裏得益於出色的支持。在這個新版本中,我收到錯誤「使用未解析標識符名稱」。此錯誤發生在流量控制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
}
那麼,您的舊代碼在1.2中無法編譯,因此我猜您的新代碼將「具有相同的用途」。舊代碼中的特定錯誤(多次)是作爲[AnyObject]的'xxx [「key」]錯誤 - xxx [「key」]是可選的。另外,你從錯誤的軌道開始 - 嘗試使用像'if let results = json [「hits」] {'這樣的結構。 請參閱https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html – Grimxn
我正在使用6.1.1。在那個版本中,舊代碼就像一個魅力。可能是在1.2中,一些可選項應該被解開。但我主要對名稱聲明感到好奇。這是正確還是錯誤? – user3398985
即使在舊版本中,我相信'json [「hits」]如果沒有這樣的入口,仍然會返回零,這就是爲什麼他們改變了語言。至於你的'name',你可以(在1.2中)使用類似'if idValue = itemDictionary [「_ id」],name = idValue [「_ fields」] [「item_name」] {' - 我建議你更新到該語言的當前版本。 – Grimxn