3

I have the following Object Array for the Place class:

class Place: NSObject {
    var distance:Double = Double()
    init(_ distance: Double) {
        self.distance = distance
    }
}


let places = [Place(1.5), Place(8.4), Place(4.5)]

I need to get the Place with the minimum distance. I tried using

let leastDistancePlace = places.min { $0.distance > $1.distance }

as per this answer for a similar question, but it gave the following error.

Contextual closure type '(Place) -> _' expects 1 argument, but 2 were used in closure body

PS:

As per @robmayoff 's answer, I tried the following in a playground, but I keep getting an error:

value of Type [Place] no member min

Please check this screenshot. enter image description here

My swift version is : Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)

Community
  • 1
  • 1
toing_toing
  • 2,334
  • 1
  • 37
  • 79
  • 2
    You should use `max` or `min` (as in the referenced answer), not `map` – Martin R Mar 24 '17 at 18:23
  • 1
    “it does not work” is not enough information. How does it fail? Do you get a compile-time error? Do you get a run-time error? Does it compile and run but produce the wrong answer? Please edit your question to include these details. – rob mayoff Mar 24 '17 at 18:27
  • 1
    Incidentally, in your attempt, you said `places.map` but the answer you linked said `places.max`. Perhaps you meant to say `places.min`? – rob mayoff Mar 24 '17 at 18:28
  • 2
    Why do you [leave a comment](http://stackoverflow.com/questions/27875778/how-to-find-the-max-value-in-a-swift-object-array/41217004#comment73101921_41217004) at the referenced answer that "it should be map" and then ask a new question that "map does not work"? – Martin R Mar 24 '17 at 18:28
  • @MartinR Actually both map and max didnt work. It maybe because I put invalid arguments into the cosure. I clarified it there. Sorry – toing_toing Mar 24 '17 at 18:32
  • @toing_toing: Better clarify your question here, before it is closed :) – Martin R Mar 24 '17 at 18:34
  • @MartinR added the error I got – toing_toing Mar 24 '17 at 18:37
  • @robmayoff added details, seems issue was with the arguments I used – toing_toing Mar 24 '17 at 18:38
  • @toing_toing: Again: You want `max`, not `map`. That compiles without problems. – Martin R Mar 24 '17 at 18:40
  • @MartinR edited. I tried `max`, but it says value of Type [Place] no member max – toing_toing Mar 24 '17 at 18:44
  • 1
    I have copied your updated code into a new Xcode 8.2.1/Swift 3 project and it compiles (and runs) without problems. – Martin R Mar 24 '17 at 18:50
  • In Swift 2.2, `min` was called `minElement`. – vacawama Mar 24 '17 at 21:20

3 Answers3

17
let leastDistancePlace = places.min { $0.distance < $1.distance }

or

let leastDistancePlace = places.min(by: { $0.distance < $1.distance })

Example:

:; xcrun swift
"crashlog" and "save_crashlog" command installed, use the "--help" option for detailed help
Welcome to Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1). Type :help for assistance.
  1>     class Place { 
  2.         var distance:Double = Double() 
  3.         init(_ distance: Double) { 
  4.             self.distance = distance 
  5.         } 
  6.     } 
  7.  
  8.  
  9.     let places = [Place(1.5), Place(8.4), Place(4.5)] 
 10.     let leastDistancePlace = places.min { $0.distance < $1.distance }
places: [Place] = 3 values {
  [0] = {
    distance = 1.5
  }
  [1] = {
    distance = 8.4000000000000004
  }
  [2] = {
    distance = 4.5
  }
}
leastDistancePlace: Place? = (distance = 1.5) {
  distance = 1.5
}
 11>  
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • please check the updated question. Its quite bizzare. I believe your solution should work, but it doesn't. – toing_toing Mar 24 '17 at 19:35
  • 3
    @toing_toing, use `minElement` instead of `min` in Swift 2.2. You really should update to Swift 3 and latest Xcode. – vacawama Mar 24 '17 at 21:25
1
let sortedPlaces = places.sorted(by: { $0.distance < $1.distance })
let first = sortedPlace.first

just use sort

Daivest
  • 66
  • 4
1

Your question is poorly worded however I think I know what you are trying to ask. The mapping function is generally used for tranformations:

let distances = places.map({ (place: Place) -> Int in
    place.distance
})

For shorthand

let distances = places.map({ $0.distance }) 

You can then use max or min on this array of integers in order to extract the value you desire.

jnewkirk
  • 391
  • 1
  • 2
  • 13