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. 「日常训练」Common Subexpression Elimination(UVa-12219)

    今天做的题目就是抱佛脚2333 懂的都懂. 这条题目干了好几天,最后还是参考别人的代码敲出来了,但是自己独立思考了两天多,还是有收获的. 思路分析 做这条题我是先按照之前的那条题目(The SetSt ...

  2. Spring 整合 Shiro

    一.引入依赖 <!-- spring start --> <dependency> <groupId>org.springframework</groupId ...

  3. libvirt保持虚拟机运行情况下修改名称

    通过virsh list命令能看到虚拟机的列表: [root@compute2 ~]# virsh list Id 名称 状态 ------------------------------------ ...

  4. Visual Studio 2005安装包

    点击下载

  5. hibernate 批量插入

    Session session = sessionFactoryUpLowLimit.openSession(); session.beginTransaction(); for(int i=0 ;i ...

  6. windows下 eclipse搭建spark java编译环境

    环境: win10 jdk1.8 之前有在虚拟机或者集群上安装spark安装包的,解压到你想要放spark的本地目录下,比如我的目录就是D:\Hadoop\spark-1.6.0-bin-hadoop ...

  7. LTE中基于S1的切换

    1:源eNodeB决定进行基于S1的切换.S1切换的原因可能是源eNodeB和目标eNodeB之间不存在X2连接,或者源eNodeB根据其他情况作出的判断. 2:源eNodeB向源MME发送Hando ...

  8. web四则运算

    目录 1.coding.net地址 2.PSP 3.Information Hiding, Interface Design, Loose Coupling 4.计算模块接口的设计与实现过程 5.计算 ...

  9. [NOIP2017 TG D2T2]宝藏(模拟退火)

    题目大意:$NOIPD2T2$宝藏 题解:正常做法:状压DP .这次模拟退火,随机一个排列,$O(n^2)$贪心按排列的顺序加入生成树 卡点:没开$long\;long$,接受较劣解时判断打错,没判$ ...

  10. 02.Java面向对象问题

    目录介绍 2.0.0.1 重载和重写的区别?重载和重写绑定机制有何区别?父类的静态方法能否被子类重写? 2.0.0.2 封装.继承.多态分别是什么? 2.0.0.3 接口和抽象类的区别是什么?接口的意 ...