2016-05-17 95 views
-1

誰知道爲什麼會發生這種情況?排序斯威夫特代碼

它給我錯誤在Array(places).sorted { $0.1 < $1.1 }.map { $0.0 }部分。

let placesByBeacons = [ 
    "12103:16951": [ 
     "Heavenly Sandwiches": 5, // read as: it's 5 meters from 
     // "Heavenly Sandwiches" to the beacon with 
     // major 6574 and minor 54631 
     "Green & Green Salads": 10, 
     "Mini Panini": 15 
    ], 
    "648:12": [ 
     "Heavenly Sandwiches": 250, 
     "Green & Green Salads": 100, 
     "Mini Panini": 20 
    ], 
    "17581:4351": [ 
     "Heavenly Sandwiches": 350, 
     "Green & Green Salads": 500, 
     "Mini Panini": 170 
    ] 
] 

func placesNearBeacon(beacon: CLBeacon) -> [String]? { 
    let beaconKey = "\(beacon.major):\(beacon.minor)" 
    if let places = self.placesByBeacons[beaconKey] { 
     let sortedPlaces = Array(places).sorted { $0.1 < $1.1 }.map { $0.0 } 
     return sortedPlaces 
    } 
    return nil 
} 
+0

'sorted' - >'sort' – Hamish

回答

0

您應該使用sort而不是sorted。您也可以讓你的函數短:

func placesNearBeacon(beacon: CLBeacon) -> [String]? { 
    guard let places = placesByBeacons["\(beacon.major):\(beacon.minor)"] else { return nil } 
    return places.sort(<).map { $0.0 } 
} 
+0

會'的.sort(<)'真上工作的元組的排列關係嗎? –