当我尝试打印包含任何对象的数组的索引时。它没有建议indexOf
功能。当我尝试手动输入其显示错误,我已附加截图。
但它正在为String of String工作。有人可以帮我解决这个问题吗?
此外,我正在显示我的代码
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.
}
}
答案 0 :(得分:4)
您可以在数组中设置所需的类型(Int
,String
等。)或在执行{arrayOfAnyObject
之前将AnyObject
转换为indexOf
{1}},像这样:
print("Index of any object array \((arrayOfAnyObject as AnyObject).index(of: "anyObject"))")