[基本解决]Swift的for循环中出错:Value of type UnsafeMutablePointer<[UIView]> (aka UnsafeMutablePointer<Array<UIView>>) has no member Generator

[背景]

代码:

        var buttons:UnsafeMutablePointer<[UIView]> = UnsafeMutablePointer<[UIView]>((tabButtonsContainerView!.subviews))
        for button in buttons {

出错:

Value of type ‘UnsafeMutablePointer<[UIView]>’ (aka ‘UnsafeMutablePointer<Array<UIView>>’) has no member ‘Generator’

如图:

Value of type UnsafeMutablePointer UIView aka UnsafeMutablePointerArrayUIView has no member Generator

[解决过程]

1.试着改为:

for button:UnsafeMutablePointer<UIView> in buttons {

又出错:

‘UnsafeMutablePointer<[UIView]>’ (aka ‘UnsafeMutablePointer<Array<UIView>>’) is not convertible to ‘UnsafeMutablePointer<UIView>’

2.参考:

Facets of Swift, Part 3: Values and References — Swift Programming — Medium

的“In Swift, classes are reference types by default, so there is no need to write pointers”

倒是突然想起来:

好像是:

对于前面用代码:

var buttons:UnsafeMutablePointer<[UIView]> = UnsafeMutablePointer<[UIView]>((tabButtonsContainerView!.subviews))

定义的buttons,好像是指针类型:

或许即是是数组,也没发挨个去枚举去访问?

所以报错说没有generator?

3.搜:

swift  Value of type UnsafeMutablePointer<[UIView]> (aka UnsafeMutablePointer<Array<UIView>>) has no member Generator

swift  Value of type UnsafeMutablePointer<[]> has no member Generator

swift  Value of type UnsafeMutablePointer array has no member Generator

参考:

Re: Value of type ‘[AnyObject]!’ has no member ‘Generator’

说是:

The variable is a ‘!!’ which means it’s implicitly unwrapped optional of an implicitly unwrapped optional of an array, so when you use it it gets unwrapped for you, once, thus leaving an implicitly unwrapped optional and optionals don’t have generators, you have to re-unwrap it again.

Value of type ‘[AnyObject]!’ has no member ‘Generator’

去加上个感叹号试试

for button in buttons! {

结果还提示我要删除掉!

note to delete declarement for unsafe

4.搜:

swift  Value of type  has no member Generator

参考:

Iterate through a String Swift 2.0 – Stack Overflow

swift2 – Error: value of type ‘String’ has no member ‘Generator’ in Swift – Stack Overflow

去找找看,此处的bottons是否有某个子属性

see button has some attibute or not

没找到。。。

5.把代码去掉指针试试:

        var buttons:[UIView] = tabButtonsContainerView!.subviews
        for button in buttons {

结果还真的,至少是可以编译了。。。

[总结]

此处,对于:

var buttons:UnsafeMutablePointer<[UIView]> = UnsafeMutablePointer<[UIView]>((tabButtonsContainerView!.subviews))

始终无法用for循环:

for button in buttons {

去访问buttons中的每一个button(UIView)

最终是,把之前加的指针去掉换成:

var buttons:[UIView] = tabButtonsContainerView!.subviews

for button in buttons {

才可以通过编译。。。

 

很明显:

原先的代码本意是指针,

这么一改,就变成了 实例对象了。

所以:如果之后的代码运行时,因此而出问题:

本意是调用UIView的指针,实际上变成调用实例对象,

而出错的话,则到时候再想办法吧。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量