[背景]
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)
如图:
[解决过程]
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
参考:
去试试用!强制展开:
待会再去试
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