http://stackoverflow.com/questions/25458548/swift-ambiguous-use-of-operator

I have just downloaded Xcode6-beta6. I am getting compiler error "ambiguous use of operator '>'" for following codes

reversed = sorted(names, { s1, s2 in s1 > s2 } )

It was working before in Xcode6-beta5.

The code is from apple swift documentationhttps://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-XID_152

Any ideas?

asked Aug 23 '14 at 4:49
 
    
What is names defined as? –  Mike S Aug 23 '14 at 4:51
    
What type is names? I just tried it successfully in a playground with the following code: let names = ["a", "b"]; let reversed = sorted(names, { s1, s2 in s1 > s2 } ) –  Gary Makin Aug 23 '14 at 5:20 
2  
After seeing your comment i tested again and found the issue. Thanks. It's the issue with variable. In the swift document "reversed" was declared once and then used in everywhere and then this issue arises only for "Implicit Returns from Single-Expression Closures" and "Shorthand Argument Names" cases. If you define new variable or constant then this error does not show up. –  moin uddin Aug 23 '14 at 14:13
    
I get the same error with var arrayToSort = ["a", "b"]; arrayToSort.sort{ $0 > $1 }. If I change the operator to less than (<) the error disappears. –  wottpal Aug 23 '14 at 22:25 

2 Answers

I had the same issue also with

if ("aa" > "bb")  {
[...]
}

and

reversed = sorted(names, { $0 > $1 })

Apparently XCode can't correctly infer the correct type "String" for the parameters, thus creating an ambiguity on the operator. My solution has been to explicitly declare the type at least one of them which also makes the code more readable. Like in:

if ("aa" as String > "bb")  {
[...]
}

reversed = sorted(names, { $0 as String > $1 })

answered Nov 12 '14 at 13:27
 

This seems to be a bug in the Foundation framework's bridging. It declares overrides of > to handle comparing a String with an NSString and an NSString and a String, but those appear (in some cases) to conflict in matching. You can get around it (for some reason) by altering your syntax a little:

reversed = sorted(names, { s1, s2 in return s1 > s2 } )
answered Nov 4 '14 at 15:34
Nate Cook
26.6k26379
 

Your Answer

Swift “ambiguous use of operator '>'”的更多相关文章

  1. Swift语言精要 - Operator(运算符重载)

    运算符重载 Swift的这一语言特性或许应该启发于C++ class Vector2D { var x : Float = 0.0 var y : Float = 0.0 init (x : Floa ...

  2. swift @AUTOCLOSURE 和 ?? ||

    * {-webkit-tap-highlight-color: rgba(0,0,0,0);}html {-webkit-text-size-adjust: none;}body {font-fami ...

  3. swift Equatable 函数签名的测试

    struct Degoo:Equatable { var lex:String var pex:String static func == (left:Degoo, right:Degoo) -> ...

  4. 一些常见warning的原因和解决方法

    在入职三周后,终于赶齐了接手项目落下两个月的项目,有了一些自己的空闲时间对项目进行整理.主要整理包括类目的整合,从原来一个系统文件夹下几百个文件整改为以MVC设计思想为原则的分文件夹整理类目,井然有序 ...

  5. JSON的BUG

    问题描述: method_cfg["input_method"][0].asString() 报错误: test1.cpp:40:46: error: ambiguous over ...

  6. Qt jsoncpp 对象拷贝、删除、函数调用 demo

    /*************************************************************************************************** ...

  7. iPhone Tutorials

    http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...

  8. c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.

    // Note: //int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of ...

  9. Swift学习(一):自定义运算符 operator

    自定义运算符仅能包含这些字符: / = - + * % < >!& | ^.~ 运算符位置: 前置运算符 prefix 中间运算符 infix 后置运算符 postfix 运算符其 ...

随机推荐

  1. LeetCode - 35. Search Insert Position(48ms)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. PAT——乙级1006:换个格式输出整数&乙级1021:个位数统计&乙级1031:查验身份证

    1006 换个格式输出整数 (15 point(s)) 让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 ...

  3. GBDT && XGBOOST

                                  GBDT && XGBOOST Outline Introduction GBDT Model XGBOOST Model ...

  4. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  5. 预处理器&预处理变量&头文件保护&条件编译

    [常见的预处理功能] #include 头文件保护符 条件编译 [预处理器] 编译之前执行的一段程序,可以部分地改变我们所写的程序 举个例子:当预处理器看到#include标记时就会用指定的头文件的内 ...

  6. MQTT在平台中的应用【本文摘自智车芯官网】

    MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分.该协议支持所有平台,几乎可以把所有联 ...

  7. lintcode-118-不同的子序列

    118-不同的子序列 给出字符串S和字符串T,计算S的不同的子序列中T出现的个数. 子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响.(比如,&q ...

  8. lintcode-57-三数之和

    57-三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 注意事项 在三元组(a, b, c),要求a <= b <= ...

  9. 在VS2012中设置默认启动

    Visual Studio 2012一个解决方案中多个项目,如果想选择哪个项目就设置哪个项目为启动项就好了. 第一种方法,工具===〉〉选项===〉〉〉项目解决方案===〉〉〉对于新的解决方案,使用单 ...

  10. BZOJ1975 SDOI2010魔法猪学院(启发式搜索+最短路+堆)

    对反图跑最短路求出每个点到终点的最短路径,令其为估价函数大力A*,第k次到达某个点即是找到了到达该点的非严格第k短路,因为估价函数总是不大于实际值.bzoj可能需要手写堆.正解是可持久化可并堆,至今是 ...