Ios 无法强制转换类型为';Swift.Array<;任何>';至';斯威夫特词典<;Swift.String,任意>';
我在数组中放置了以下词典Ios 无法强制转换类型为';Swift.Array<;任何>';至';斯威夫特词典<;Swift.String,任意>';,ios,arrays,swift,dictionary,Ios,Arrays,Swift,Dictionary,我在数组中放置了以下词典 //Collections var myShotArray = [Any]() var myShotDictionary = [String: Any]() myShotDictionary = ["shotnumber": myShotsOnNet, "location": shot as Any, "timeOfShot": Date(), "period": "1st", "result": "shot"] myShotArray.append(my
//Collections
var myShotArray = [Any]()
var myShotDictionary = [String: Any]()
myShotDictionary = ["shotnumber": myShotsOnNet, "location": shot as Any, "timeOfShot": Date(), "period": "1st", "result": "shot"]
myShotArray.append(myShotDictionary as AnyObject)
然后我将数组传递给我的tableview
myGoalieInforamtionCell.fillTableView(with: [myShotArray])
在我看来
var myShotArray = [Any]()
func fillTableView(with array: [Any]) {
myShotArray = array
tableView.reloadData()
print("myShotArray \(myShotArray)")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("ShotInformationTableViewCell", owner: self, options: nil)?.first as! ShotInformationTableViewCell
let positionInArray = myShotArray[indexPath.row] as! [String : Any] //Could not cast value of type 'Swift.Array<Any>' (0x103991ac0) to 'Swift.Dictionary<Swift.String, Any>' (0x1039929b0).
cell.myGoalieShotInformationShotNumberLabel.text = positionInArray["shotnumber"]! as? String
return cell
}
var myShotArray=[Any]()
func fillTableView(带数组:[Any]){
myShotArray=array
tableView.reloadData()
打印(“myShotArray\(myShotArray)”)
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=Bundle.main.loadNibNamed(“ShotInformationTableViewCell”,所有者:self,选项:nil)?。首先是!ShotInformationTableViewCell
让positionInArray=myShotArray[indepath.row]作为![String:Any]//无法将类型为“Swift.Array”(0x103991ac0)的值强制转换为“Swift.Dictionary”(0x1039929b0)。
cell.myGoalieShotInformationShotNumberLabel.text=PositionArray[“shotnumber”]!as?字符串
返回单元
}
为什么我会在主题中出现上述错误
提前感谢。当您调用
MyGoalieInformationCell.fillTableView
时,您正在传递[myShotArray]
-这些方括号表示您已将myShotArray
放在另一个数组中,因此您实际传递给fillTableView
的是[[String:Any]]
-字典数组的数组
你可以通过简单地移除这些括号来解决眼前的问题
myGoalieInforamtionCell.fillTableView(with: myShotArray)
然而,你有太多的任何。您应该利用Swift强大的键入功能,这将避免此类错误
我建议您使用Struct
而不是字典来存储数据,这样您就可以正确地键入内容。比如:
enum Period {
case first
case second
case third
case fourth
}
struct ShotInfo {
let shotNumber: Int
let location: String // Not sure what this type should be
let timeOfShot: Date
let period: Period
let result: Bool
}
var myShotArray = [ShotInfo]()
let shot = ShotInfo(shotNumber: myShotsOnNet, location: shot, timeOfShot: Date(), period: .first, result: true}
myShotArray.append(shot)
myGoalieInforamtionCell.fillTableView(with: myShotArray)
func fillTableView(with array: [ShotInfo]) {
myShotArray = array
tableView.reloadData()
print("myShotArray \(myShotArray)")
}
如果您有此错误,并且错误地说了fillTableView(with:[myShotArray])
Xcode会立即告诉您参数类型与预期类型不匹配,这比在程序崩溃时在运行时发现错误要好得多。此处:
myGoalieInforamtionCell.fillTableView(with: [myShotArray])
您正在将数组包装到一个附加数组中,因此在访问数组以填充单元格时,您将获得数组而不是字典
应该是:
myGoalieInforamtionCell.fillTableView(with: myShotArray)
至少您应该将myShotArray
声明为[[String:Any]]
,并将参数fillTableView
更改为[[String:Any]]
,这样编译器就会发现这个错误。它还允许您删除抛出错误的强制转换
您真的应该创建一个结构/类,并传递这些结构/类的数组,而不是字典。感谢您对enum和struct的详细解释,非常感谢。