I have a really weird problem. I am sorting an array of NSDictionary objects in my application, but it only works correctly when the application starts from Xcode. Once I distribute the application and install and run it on the device, sorting no longer works.
Here the code can be run on the playground, with some examples of NSDictionary objects. The code in the application is the same.
import UIKit let p1 = NSDictionary(objects: ["Zoe", 32], forKeys: ["name", "age"]) let p2 = NSDictionary(objects: ["Adrian", 54], forKeys: ["name", "age"]) let p3 = NSDictionary(objects: ["Jeff", 23], forKeys: ["name", "age"]) let p4 = NSDictionary(objects: ["", 66], forKeys: ["name", "age"]) let p5 = NSDictionary(objects: [23], forKeys: ["age"]) let persons = [p1,p2,p3,p4,p5] let sortedPersons = persons.sorted { (p1, p2) -> Bool in (p2["name"] as? String) > (p1["name"] as? String) }
As you can see, sorting on the playground works correctly. Does anyone know what could be wrong?
Update
I found that the Swift optimization level is causing the problem. Setting this parameter to -O (the fastest) will cause the sorting to fail. Setting it to -Onone will cause sorting to work correctly.
Does anyone have any suggestions for changing the code, so it will work with -O optimization?
Update 2
I published a bug report in Apple. So far, I'm using NSSet to sort an array, which seems to work fine.
Last update
I could not reproduce this since Xcode 6.1.1
ios objective-c swift
RenΓ©
source share