类型 "Any?" 没有下标成员(尝试在 VC 中更新图像视图)


 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        animations.image = exerciseList[indexPath.row]["animations"] as? [UIImage]
}

我不断收到类型"任何?"错误。我知道这是因为我使用字典将值传递给 TBC。我正在尝试返回包含练习动画的 UI 图像数组。我试图将练习列表本身转换为 [字符串:任意] 然后添加 UI 图像,但我遇到了另一个错误。有人知道我搞砸了吗?

值按原样存储

更新:

      if selectedIndexPath.item == 1 {
    let squatDictionary = ["name" : "Squat" , "animations" : squatAnimations] as [String : Any]
    let jSquatDic = ["name" : "Jump Squat", "animations" : jumpSquatAnimations] as [String : Any]
                animationVC.exerciseList = [squatDictionary, jSquatDic]

我的练习列表获取数据声明为:

    var exerciseList = [[String : Any]]()
可能是

您遇到误导性错误,问题是您正在设置UIImageView的属性image [UIImage]表示UIImage数组而不是单个UIImage对象。如果要使用UIImageView对多个图像进行动画处理,则需要使用animationImages使用UIImageView对图像进行动画处理。

animation.animationImages = exerciseList[indexPath.row]["animations"] as? [UIImage]
animation.animationDuration = 0.5
//If you don't set animationRepeatCount it will animate infinitely 
//animation.animationRepeatCount = 5 //Set if you don't want to animate infinitely
//To start animation call startAnimating() on imageView
animation.startAnimating()

最新更新