gpt4 book ai didi

ios - 缺少 Swift 3 数组 indexOf 函数

转载 作者:搜寻专家 更新时间:2023-10-31 08:30:29 25 4
gpt4 key购买 nike

当我尝试打印包含任何对象的数组的索引时。它不建议使用 indexOf 函数。当我尝试手动输入时,它显示错误,我附上了屏幕截图。

但它适用于字符串数组。谁能帮我解决这个问题?

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.
}


}

最佳答案

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

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

关于ios - 缺少 Swift 3 数组 indexOf 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44427779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com