【问题标题】:Using indexOf() with an array of type UIButton将 indexOf() 与 UIButton 类型的数组一起使用
【发布时间】:2015-08-05 21:35:33
【问题描述】:

我在此链接上关注名为“开始开发 iOS 应用程序 (Swift)”的教程。 https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson5.html#//apple_ref/doc/uid/TP40015214-CH19-SW1

据我所知,我完全按照教程进行操作,但我对以下代码块中最后一行代码的问题感到困惑。

        var ratingButtons = [UIButton]()
        ...
        // MARK: Button Action
        func ratingButtonTapped(button: UIButton){
            //print("Button pressed ????")
            rating = ratingButtons.indexOf(button)! + 1
        }

我对 iOS 开发非常陌生,这里有一些东西不明白。

首先,我收到错误“[(UIButton)]”没有名为“indexOf”的成员。 UIButton 周围的括号和括号有什么意义吗?另外,据我了解 ratingButtons 是一个数组,所以 indexOf 在这种情况下不应该正常工作吗?

【问题讨论】:

    标签: swift uibutton indexof


    【解决方案1】:

    indexOf 是从 Swift 2.0 开始创建的。您不需要更新您的 Xcode,只需使用 find 方法,

    rating = find(ratingButtons, button)! + 1
    

    【讨论】:

      【解决方案2】:

      该教程是关于 Swift 2.0 的。 indexOf 存在于 Swift 2.0 中。您使用的是 Swift 1.2,所以它不适合您。更新到 Xcode 7,以便您可以使用 Swift 2.0。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        相关资源
        最近更新 更多