缺少Swift 3数组indexOf函数

时间:2017-06-08 06:03:20

标签: ios arrays swift any

当我尝试打印包含任何对象的数组的索引时。它没有建议indexOf功能。当我尝试手动输入其显示错误,我已附加截图。

但它正在为String of String工作。有人可以帮我解决这个问题吗?

enter image description here

此外,我正在显示我的代码

import UIKit

class ViewController: UIViewController {

    var arrayOfAnyObject: [Any] = [Any]()
    var arrayOfStringObject: [String] = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()


         print("Index of string object array \(arrayOfStringObject.index(of: "anyString")!)")

         print("Index of any object array \(arrayOfAnyObject.index(of: "anyObject")!)")




        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

1 个答案:

答案 0 :(得分:4)

您可以在数组中设置所需的类型(IntString等。)或在执行{arrayOfAnyObject之前将AnyObject转换为indexOf {1}},像这样:

print("Index of any object array \((arrayOfAnyObject as AnyObject).index(of: "anyObject"))")