If statement Swift:找不到接受提供的参数的“>”的重载

If statement Swift:找不到接受提供的参数的“>”的重载,if-statement,swift,conditional-operator,If Statement,Swift,Conditional Operator,我知道这个问题已经被问到了,在我的例子中,其他各种符号取代了“>”符号。但他们都没有解决我的问题。我需要做的是检查搜索字符串的长度是否大于2,如果大于2,则执行搜索。为此,我编写了以下if条件: if countElements(searchString) > 2 {} 其中searchString是字符串对象。但这一行产生了错误: Could not find an overload for '>' that accepts the supplied arguments 我发现

我知道这个问题已经被问到了,在我的例子中,其他各种符号取代了“>”符号。但他们都没有解决我的问题。我需要做的是检查搜索字符串的长度是否大于2,如果大于2,则执行搜索。为此,我编写了以下if条件:

if countElements(searchString) > 2 {}
其中searchString是字符串对象。但这一行产生了错误:

Could not find an overload for '>' that accepts the supplied arguments
我发现Swift不会隐式转换值,所以我尝试了以下代码

if Int(countElements(searchString)) > 2 {}
仍然导致相同的问题。这里怎么了

编辑

searchString来自此委托:

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {

    if countElements(searchString) > 2
    {

    }
    return false
 }

问题不在于问题中包含的代码。如果你在操场上尝试以下方法,效果很好:

var searchString = "foo"

if countElements(searchString) > 2 {
  println("It's a long string!")
}

你能提供一个非常简单的例子来说明你的问题吗?

我解决了它。实际问题是swift如何接受变量。在searchString之后添加感叹号解决了问题。更正代码:

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {

    if countElements(searchString!) > 2
    {

    }
    return false
}

您是否尝试过:if countElementssearchString>Int2{}?同样的问题。已尝试请查看我的编辑。searchString来自一个delegateWell,实际上并不奇怪。这就是斯威夫特的工作原理。我们使用的是!在swift中,表示该值不是null,并且它肯定有一些值。当它是隐式展开的可选值时,则不是。为什么我们要强制打开已经隐式打开的东西呢?嗯。。现在这令人困惑。