ios - 如何检查字符串所在的数字数组。使用swift

标签 ios arrays xcode swift dictionary

我如何让文本字段输入返回文本所在的数组索引,而不是像下面那样手动执行,因为我将在数组中有数百个项目(颜色只是一个例子)?

@IBOutlet weak var label:UILabel

@IBOutlet weak var textField:UITextField


var arrayOfColors:[String] = ["Blue", "Black", "Yellow", "Purple"]


if textFeild.text == "Blue" {

println("array index 0")
}

else if textFeild.text == "Yellow" {

println("array index 3")
}

最佳答案

这给了你一个可选的,所以准备好在必要时处理 nil

find(arrayOfColors, textFeild.text!) //returns the index (0) if text is "Blue"

关于ios - 如何检查字符串所在的数字数组。使用swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29306336/

相关文章:

iOS 7 创建一个像 Photo 和 AppStore 一样的底部 UIToolbar

ruby - 验证数组是升序还是降序

objective-c - Xcode 4 和分支

ios - 如何更改App Store 描述中的 "Languages"?

iphone - iOS 应用程序 "next"键不会转到下一个文本字段

ios - NSAttributedString 在字符串末尾更改颜色

ios - swift:collectionView 单元格中的标签

objective-c - 绘制 UIBezierPaths 的程序?

python - 在具有排除索引的数组中找到最大值

c - 在函数参数中传递数组时访问数组的最后一个元素显示垃圾值