使用 CloudKit CKQuery,您可以过滤距离(参见示例)。但是你怎么能按距离排序呢?像位置ASC。位置上的排序描述符返回错误: query.sortDescriptors = [NSSortDescriptor(key: "distanceToLocation", ascending: true)] :

  static func getNearbySpots(location : CLLocation, completionHandler: (spots : CKRecord[]) -> Void) {
        let predicate = NSPredicate(format: "distanceToLocation:fromLocation:(location, %@) < 10000", location)

        let queryOperation = CKQueryOperation(query: query)
        var results : CKRecord[] = []

        queryOperation.recordFetchedBlock = {
            results += $0
        }

        queryOperation.queryCompletionBlock = {
            if $1 {
                NSLog($1.description,[])
                abort()
            } else {
                completionHandler(spots: results)
            }
        }

        self.publicDB().addOperation(queryOperation)
    }

最佳答案

好的,找到了。有一个 CKLocationSortDescriptor,您可以在其中指定位置。

 query.sortDescriptors = [CKLocationSortDescriptor(key: "location", relativeLocation: location)]

关于cloudkit - CKQuery 的 distanceToLocation 上没有 NSSortDescriptor?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24360118/

10-11 18:36