1

I am converting my code from swift 3.2 to swift 4 and I am getting error for this code

self.arrData = json.sorted{ $0.0.1["chkincount"].doubleValue > $0.1.1["chkincount"].doubleValue }.map { $0.1 }

Contextual closure type '((String, JSON), (String, JSON)) -> Bool' expects 2 arguments, but 1 was used in closure body

How to fix this ?

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
  • Does this answer your question? ["contextual closure type expects 2 arguments" error when using reduce in Swift 4](https://stackoverflow.com/questions/46432013/contextual-closure-type-expects-2-arguments-error-when-using-reduce-in-swift-4) – pkamb Sep 03 '20 at 00:47
  • https://stackoverflow.com/help/minimal-reproducible-example – Chris Sep 03 '20 at 08:00
  • @pkamb I already got answer to my question and the link that you are referring was to there at the I posted my question. – Varun Naharia Sep 03 '20 at 08:36

1 Answers1

2

In sorted closure changed $0.0.1 with $0.1 to access first closure argument and $0.1.1 with $1.1 to access second closure argument after making this changes you all set to go.

self.arrData = json.sorted{ $0.1["chkincount"].doubleValue > $1.1["chkincount"].doubleValue }.map { $0.1 }
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Can you please explain why this code is working in swift 4 and above is not working ? – Varun Naharia Jun 16 '17 at 07:08
  • @VarunNaharia I have already write in my answer that you haven't access the second argument `$1` in your closure so you need to access that instead of `$0.1` it is `$1` as of `$0` and `$1` represent `(String, JSON)` not the `((String,JSON),(String,JSON))` – Nirav D Jun 16 '17 at 07:10
  • @VarunNaharia May be they have changed that in Swift 4. – Nirav D Jun 16 '17 at 07:18
  • 1
    https://github.com/apple/swift-evolution/blob/master/proposals/0110-distingish-single-tuple-arg.md – Hamish Jun 16 '17 at 08:02