Swift “ambiguous use of operator '>'”

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


3down votefavorite

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?

swift xcode6


shareimprove this question

asked Aug 23 ‘14 at 4:49

moin uddin
153110

 

    

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 

add a comment

2 Answers

activeoldestvotes


up vote4down vote

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 })


shareimprove this answer

edited Nov 12 ‘14 at 13:43

answered Nov 12 ‘14 at 13:27

Michele Dall‘Agata
133112

 
add a comment


up vote2down vote

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 } )

shareimprove this answer

answered Nov 4 ‘14 at 15:34

Nate Cook
26.6k26379

 
add a comment

Your Answer

Swift “ambiguous use of operator '>'”

时间: 2024-11-02 11:45:26

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

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

运算符重载 Swift的这一语言特性或许应该启发于C++ class Vector2D { var x : Float = 0.0 var y : Float = 0.0 init (x : Float, y: Float) { self.x = x self.y = y } func +(left : Vector2D, right: Vector2D) -> Vector2D { let result = Vector2D(x: left.x + right.x, y: left.y + r

swift @AUTOCLOSURE 和 ?? ||

* {-webkit-tap-highlight-color: rgba(0,0,0,0);}html {-webkit-text-size-adjust: none;}body {font-family: Arial, Helvetica, sans-serif;margin: 0;color: #333;word-wrap: break-word;}h1, h2, h3, h4, h5, h6 {line-height: 1.1;}img {max-width: 100% !importan

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

在入职三周后,终于赶齐了接手项目落下两个月的项目,有了一些自己的空闲时间对项目进行整理.主要整理包括类目的整合,从原来一个系统文件夹下几百个文件整改为以MVC设计思想为原则的分文件夹整理类目,井然有序了很多,也不需要再用查找关键字来寻找想要找的类了,中间因为类目文件位置的修改而出现了很多问题.其次还包括一些代码的整合,包括一些多个类中都需要使用的代码,我们可以创建一个工具类来封装调用,或者使用一个根类来集成代码. 在做完了以上工作后,我又把关注重点放在了150多个warning之上.作为一个强迫

c++primer(第五版) 第十三章 拷贝控制习题答案

纯原创    转载请注明出处:http://blog.csdn.net/axuan_k 13.2    13.3   13.4    13.5 #include<iostream> using namespace std; class Point{ int a; }; Point global; //13.4 Point foo_bar(Point arg) //1 { Point local = arg, *heap = new Point(global); //2,3 *heap = lo

C++ class without pointer

  写在前面 终于放假了,可以自由安排时间了,既然已经决定放弃做实验,不是不想坚持,更想正常毕业,可是这个实验偶然性太大,最主要的是不可重现,所以也没意义,也没有理论支持,当然角度新,如果测量出一个好 的实验效果,或许我也通过答辩而顺利毕业,可我不想为了毕业而毕业,再说都做了一年多了,花了大量时间在上面,过去几个月一直焦心毕业问题,投入的时间更多,还放弃了秋招,现在,想想实在是不明智的选择,现在,我只想做我自己想做的事,be a game changer.从今天晚上开始,我先简单复习C++,虽然

Swift在for循环中报错&#39;++&#39; is not a binary operator

最近刚开始学swift,遇到了一个非常诡异的问题.是在写for循环的时候出现语法错误.代码如下: for var i = 0; i < 10; i++{ println("hello world") } 按理说这是Swift里最简单的for循环的使用了.但是编译器还是报了两个错: '++ is not a binary operator' Operator is not a known binary operator 虽然苹果在官方文档里面说,建议通过++i这种方式使用自增运算符

operator is not a known binary operator swift 语法错误笔记

operator is not a known binary operator swift 语法错误笔记 error: operator is not a known binary operator for x in 1..10 版本更新,开区间  改为 "..<" var arr = String[]() array types are now written with the brackets around the element type 版本更新,改为var arr:[S

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

自定义运算符仅能包含这些字符: / = - + * % < >!& | ^.~ 运算符位置: 前置运算符 prefix 中间运算符 infix 后置运算符 postfix 运算符其他配置 结合性 associativity 可取值范围 left,right和none 优先级 precedence 可取值范围 0-255 系统内置运算符结合性质及优先级 求幂相关(无结合,优先级160) << 按位左移(Bitwise left shift) >> 按位右移(Bit

5.swift大法-运算符 Operator

算术运算符 条件运算符 var score = 65 var rate = score >= 60 ? "及格" : "不及格" // rage = "及格"