将 indexOf() 与 UIButton 类型的数组一起使用
Using indexOf() with an array of type UIButton
我正在学习一个名为 "Starting Developing iOS Apps (Swift)" 的教程 link。 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 2.0 的。 indexOf
存在于 Swift 2.0 中。您正在使用 Swift 1.2,因此它不适合您。更新到 Xcode 7 以便您可以使用 Swift 2.0.
indexOf 是从 Swift 2.0 开始创建的。您不需要更新 Xcode,只需使用 find
方法,
rating = find(ratingButtons, button)! + 1
我正在学习一个名为 "Starting Developing iOS Apps (Swift)" 的教程 link。 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 2.0 的。 indexOf
存在于 Swift 2.0 中。您正在使用 Swift 1.2,因此它不适合您。更新到 Xcode 7 以便您可以使用 Swift 2.0.
indexOf 是从 Swift 2.0 开始创建的。您不需要更新 Xcode,只需使用 find
方法,
rating = find(ratingButtons, button)! + 1