【问题标题】:'[(UIButton)]' does not have a member named 'enumerate' can anyone guide me for this error?'[(UIButton)]' 没有名为 'enumerate' 的成员,谁能指导我解决这个错误?
【发布时间】:2015-08-20 07:37:21
【问题描述】:
for (index, button) in ratingButtons.enumerate() <--getting my error here
                {
                    buttonFrame.origin.x = CGFloat(index * (44 + 5))
                    button.frame = buttonFrame
                }
            }

我也试过 enumerate(ratingButtons) 但它没有在数组中添加按钮

【问题讨论】:

    标签: ios swift enumerate


    【解决方案1】:

    取出.enumerate(),它应该可以工作,还必须将(index, button) 更改为button,因为该数组仅包含 UIButtons,而不包含索引和按钮的元组。如果需要索引,则必须手动计算数组的迭代次数,否则设置正常的非快速枚举 for 循环并手动获取索引处的按钮

    【讨论】:

    • 它给了我这个错误找不到接受类型为“(_)”的参数列表的“CGFloat”类型的初始化程序
    • 不确定你在做什么,但如果我在我的代码中使用var x = CGFloat(45),它编译得很好
    • 没问题,你应该把它标记为答案然后:)
    【解决方案2】:

    下面的代码对我有用

    var idx = 0
    var buttonFrame = CGRect(x: 0, y: 0, width: 44, height: 44)
    for button in ratingButtons {
        buttonFrame.origin.x = CGFloat(idx * (44 + 5))
        button.frame = buttonFrame
        idx = idx + 1
    }
    

    【讨论】:

      【解决方案3】:

      只需将 ratingButtons.enumerate() 替换为 enumerate(ratingButtons)
      这是枚举的正确方法

          for (index, button) in enumerate(ratingButtons)
          {
      
              buttonFrame.origin.x = CGFloat(index * (buttonSize + 5))
              button.frame = buttonFrame
          }
      

      例如 enumerate() 函数返回项目的索引及其值

      import Cocoa
      
      var strarr = [String]()
      
      strarr.append("Mango")
      strarr.append("Apple")
      strarr += ["Banana"]
      
      for (index, item) in enumerate(strarr) {
         println("Value at index = \(index) is \(item)")
      }
      

      索引 = 0 处的值是芒果
      索引 = 1 处的值是 Apple
      索引 = 2 处的值是香蕉

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2019-09-26
        相关资源
        最近更新 更多