最新消息:20210917 已从crifan.com换到crifan.org

[已解决]Swift代码出错:Cannot subscript a value of type [UIViewController] with an index of type UInt

Swift crifan 4510浏览 0评论
[背景]
swift中的代码:
var viewControllers = [UIViewController]()
...

let toViewController:UIViewController = self.viewControllers[newSelectedIndex]
出错:
Cannot subscript a value of type ‘[UIViewController]’ with an index of type ‘UInt’
并且还有提示:
Overloads for ‘subscript’ exist with these partially matching parameter lists: (Int), (Range<Int>), (Range<Self.Index>), (Self.Index)
如图:
Cannot subscript a value of type UIViewController with an index of type UInt
[解决过程]
1.搜:
swift array Cannot subscript a value of type [UIViewController] with an index of type UInt
swift array Cannot subscript a value of type  with an index of type UInt
参考:
去试试用!强制展开:
待会再去试
2.参考:
看了半天,觉得还是不对。
算了,先去改为Int试试:
let toViewController:UIViewController = self.viewControllers[Int(newSelectedIndex)]
果然可以通过编译了。
[总结]
此处代码:
self.viewControllers[newSelectedIndex]
出现:
Cannot subscript a value of type [UIViewController] with an index of type UInt
的问题,实际上是:
此处的newSelectedIndex
而默认本身支持的subscript只支持int
所以改为:
self.viewControllers[Int(newSelectedIndex)]
即可。

转载请注明:在路上 » [已解决]Swift代码出错:Cannot subscript a value of type [UIViewController] with an index of type UInt

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
95 queries in 0.185 seconds, using 23.33MB memory