Swift "Ambiguous use of operator ' > '"

Source: Internet
Author: User

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 is 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 at 4:49Moin Uddin
1531
 &nbs P;  
what is  names   Defined as? –  mike s aug "at 4:51
 &nbs P;  
what type is  names ? I just tried it successfully in a playground with the following code:  let names = ["A", "B"]; let reversed = so rted (names, {s1, s2 in S1 > s2})  –  gary makin < Span class= "Relativetime-clean" title= "2014-08-23 05:20:12z" >aug "at 5:20  
after seeing your comment I tested again and found the issue. Thanks. It ' s the issue with variable. In the swift document "reversed" is declared once and then used in everywhere and then this issue arises only for "implic It 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" 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 in 22:25
Add a Comment
2 Answersactiveoldestvotes
Up vote4down vote

I had the same issue also with

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

and

Reversed = sorted (names, {$ > $})

Apparently XCode can ' t correctly infer the correct type "String" for the parameters, thus creating a ambiguity on the ope Rator. My solution have 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"   { [...]  }            /span>         

Reversed = sorted (names, {$ as String > $ })

ShareImprove this answer Edited at 13:43 answered "at 13:27Michele Dall ' Agata
1331
Add a Comment
Up vote2down vote

The

This seems is 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 = (names, { S1, S2 in  S1 > S2 }              
ShareImprove this answer answered Nov 4 ' at 15:34Nate Cook
26.6k2
Add a Comment

Swift "Ambiguous use of operator ' > '"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.