Does anyone know how to do this in Swift 2? strings don’t have the length method anymore, and so I tried to use name.characters.count for the key and I got the following error:
Unfortunately, the update doesn’t seem to cover any changes required to complete the challenges.
Even more bizarrely, setting name.length as the sort key is working for me in Xcode 7.1, even though it doesn’t let me call length on a String in a playground!
I wonder if .length still works because that is still a valid call on an NSString. Perhaps these values are treated as NSString somewhere between the Employee class and the Table View.
Just blasting through a .length didn’t work for me. I’m wondering if in your class definition you actually have it defined as var name : NSString? ="" or something. Anyway, I decided to just make a computed property, which worked, though I have no idea if it’s optimal:
var nameLength : Int
{
get
{
let retVal = name?.characters.count ?? 0
return retVal
}
}
For what it’s worth… I’m running Xcode 7.2 and “name.length” for the Sort Key worked fine; however, I’m not sure if it’s been said already but you also have to change the Selector from “caseInsensitiveCompare:” (for actual names) to simply “compare:” (for the numeric values returned by length). This may have been obvious and done by everyone experiencing issues previously, but I just wanted to explicitly state it.
I changed the employee model’s name property to be String instead of an optional String. Oddly, using the key path name.length still worked. It must be toll-free bridging to an NSString for me, because a Swift String does not have that selector. But it finally works!