0
從Swift 2.x遷移到Swift 3時,出現錯誤「'String!'是不可轉換爲'字符串'「在線至極012Swift 2.x swift 3,XCode抱怨錯誤:'String!'不能轉換爲'String'
self.cartItemController.textCombinationsToDisplayInCart[pCombinationsPickerView.combinationName as String]=combinationValue["Name"]
是否有任何一個想法如何糾正?
func setTextCombinationsToDisplayInCart(_ pCombinationsPickerView:CombinationsPickerView,pRow:Int)
{
var combinationValue:[NSString : NSObject]=pCombinationsPickerView.combinationValues[pRow] as! [NSString : NSObject]
if(pCombinationsPickerView.combinationID != 1)//Personnalisation
{
self.cartItemController.textCombinationsToDisplayInCart[pCombinationsPickerView.combinationName as String]=combinationValue["Name"]
}
}
我用[pCombinationsPickerView.combinationName as!替換:[pCombinationsPickerView.combinationName as String]字符串],但現在它警告:「從'字符串強制轉換!'到'字符串'總是成功的,你是否想用'as'?而Xcode提議用'as'替換。按'as'... – Zipette
問題可能出現在右側,'combinationValue [「Name」]'是'NSString',你需要使用'as'轉換爲 –
非常感謝,解決了這個錯誤:我通過self.cartItemController.textCombinationsToDisplayInCart [pCombinationsPickerView.combinationName as NSString] = combinationValue [「Name」]替換self.cartItemController.textCombinationsToDisplayInCart [pCombinationsPickerView.combinationName as String] = combinationValue [「Name」] – Zipette