2

I am trying to get my head around ARKit and augment apples ARKitInteraction example which can be found here. My models load correctly however, the objects aren't listed in alphabetical order. I have put my objects in order inside the models.scnassets but that doesn't seem to fix it.

Lets say the model names are: A,B,C,D,E The list is being ordered like this: E,A,B,D,C

How would I sort the order of the models?

The model names are derived from here:

var modelName: String {
    return referenceURL.lastPathComponent.replacingOccurrences(of: ".scn", with: "")
}

And the model objects are being loaded from here:

static let availableObjects: [VirtualObject] = {

    let modelsURL = Bundle.main.url(forResource: "Models.scnassets", withExtension: nil)!
    let fileEnumerator = FileManager().enumerator(at: modelsURL, includingPropertiesForKeys: [])!

    return fileEnumerator.compactMap { element in
        let url = element as! URL

        guard url.pathExtension == "scn" && !url.path.contains("lighting") else { return nil }

        return VirtualObject(url: url)
    }
}()
spoax
  • 471
  • 9
  • 29
  • `return fileEnumerator.compactMap{...}.sorted()` ? – Larme Sep 23 '18 at 16:19
  • Do I add this to my modelName var? – spoax Sep 23 '18 at 16:21
  • 1
    In the `availableObjects` construction, do `return fileEnumerator.compactMap { element in //What you already have... }.sorted({by: $0.modelName < $1.modelName })` instead (forgot about the extension). That should do the trick, if `modelName` the a property of `VirtualObject`. – Larme Sep 23 '18 at 16:24
  • Unfortunately that doesn't work. I am getting these warnings: 1) **Consecutive statements on a line must be separated by ';'** 2) **Expected expression** 3) **Use of unresolved identifier 'by'** – spoax Sep 23 '18 at 16:36
  • 2
    `sorted(by: {$0.modelName < $1.modelName })`, misplace the `{`.See there: https://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value – Larme Sep 23 '18 at 16:38

0 Answers0