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

最新文章

  1. SQL Server删除distribution数据库
  2. 关于subGradent descent和Proximal gradient descent的迭代速度
  3. 快速入门系列--MVC--06视图
  4. Sublime Text插件:HTML+CSS+JAVASCRIPT+JSON快速格式化[转]
  5. nRF51822之模拟IIC
  6. JQ添加移除css样式
  7. stm32 IAP + APP ==&gt;双剑合一
  8. (转)CentOS 6.5下Redis安装详细步骤
  9. Leetcode:find_minimum_in_rotated_sorted_array
  10. 数独 (dfs)
  11. 分布式进阶(三)Ubuntu 14.04 之JDK安装
  12. Spring Boot应用总结更新
  13. C#泛型创建实例
  14. vscode 常用插件
  15. dfs常见的配置文件中的value与description(重要)
  16. 论EFMS模拟量部分采集电路的修改
  17. 手把手教你开发chrome扩展
  18. 关于Mybatis的Example(and ,or )应用
  19. Hive HQL基本操作
  20. threejs三角形Geometry的顶点时针顺序会导致三角形看不见

热门文章

  1. 讨伐Cucumber行为驱动
  2. 第一篇 Charles的配置及相关使用
  3. Python之tornado框架原理
  4. ubuntu 14.04 安装opencv 3.0 测试
  5. 简明Python3教程 1.介绍
  6. PokeCats开发者日志(六)
  7. 【考试记录】4.8 Path (网络流 —— 劲题)
  8. 洛谷 P2218 [HAOI2007]覆盖问题 解题报告
  9. 理解JavaScript的function
  10. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D