4

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)

12
  • 2
    You should use max or min (as in the referenced answer), not map Commented Mar 24, 2017 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. Commented Mar 24, 2017 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? Commented Mar 24, 2017 at 18:28
  • 2
    Why do you leave a comment at the referenced answer that "it should be map" and then ask a new question that "map does not work"? Commented Mar 24, 2017 at 18:28
  • 1
    I have copied your updated code into a new Xcode 8.2.1/Swift 3 project and it compiles (and runs) without problems. Commented Mar 24, 2017 at 18:50

3 Answers 3

18
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>  
Sign up to request clarification or add additional context in comments.

2 Comments

please check the updated question. Its quite bizzare. I believe your solution should work, but it doesn't.
@toing_toing, use minElement instead of min in Swift 2.2. You really should update to Swift 3 and latest Xcode.
1
let sortedPlaces = places.sorted(by: { $0.distance < $1.distance })
let first = sortedPlace.first

just use sort

3 Comments

If you only need the the smallest (or largest) element then max/min is more efficient than sorting the array.
Or min(by:). O(n) rather than O(nlog(n)).
oh, my bad, forgot about min =)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.