简体   繁体   English

使用 Swift 在数组中搜索字符串的索引

[英]Search the index of an String in Array using Swift

A simple question but a big problem with the Apple Watch.一个简单的问题,但 Apple Watch 的一个大问题。

I am searching the index of an element in an Array.我正在搜索数组中元素的索引。 But the code gives me nil .但是代码给了我nil

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    let temperatur = context as! String

    let SliderData = ["off", "comfort", "eco","5.0", "5.5", "6.0", "6.5"]  

    println(toString(temperatur.dynamicType))
    println(find(SliderData, "\(temperatur)") )
}

The array does not contain the string contained in the variable "temperatur" which is why you are not getting an index back.该数组不包含变量“temperatur”中包含的字符串,这就是您没有取回索引的原因。

If the value is already a string then you don't need to use "\\(temperatur)" You can use this line instead:如果该值已经是一个字符串,那么你不需要使用"\\(temperatur)"你可以使用这一行:

println(find(SliderData, temperatur) )

您正在搜索字符串“temperatur”——这显然不在数组中——而不是变量temperatur的内容

println(find(SliderData, temperatur) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM