gpt4 book ai didi

ios - 使用 UIPicker 从 NSArray 获取 titleForRow - Swift

转载 作者:可可西里 更新时间:2023-11-01 00:53:47 27 4
gpt4 key购买 nike

我正在尝试使用下面的 UIPickerViewDelegate 从我的数组中返回对象:

var myArr = NSArray()

将对象添加到我的数组

self.myArray = NSArray(objects: "Mr", "Mrs", "Miss", "Ms", "Dr", "Master", "Rev",
"Fr", "Atty", "Prof", "Hon")

这是我的代表

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
var string = NSString(coder: self.myArray.objectAtIndex(row) as NSCoder)
return string
}

但是这会引发异常。我该如何完成这个简单的任务?

最佳答案

完全基于您的问题,并假设没有您未提及的相关内容,摆脱编码器的东西:

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return self.myArray.objectAtIndex(row) as NSString
}

因为这是一个 swift 程序,你也不应该使用 NSArray,因为这会导致类型歧义:

var myArr = ["Mr", "Mrs", "Miss", "Ms", "Dr", "Master", "Rev",
"Fr", "Atty", "Prof", "Hon"]

现在您的委托(delegate)方法变得更加清晰:

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return myArray[row]
}

关于ios - 使用 UIPicker 从 NSArray 获取 titleForRow - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25311291/

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