I'm trying to use NSPredicate like so:
let namePredicate = NSPredicate(format: "(username BEGINSWITH[c] $word)")
if prefix == "@" {
if countElements(word) > 0 {
suggestionDatasource.users = (suggestionDatasource.users as NSArray).filteredArrayUsingPredicate(namePredicate!.predicateWithSubstitutionVariables(["word" : word])) as [User]
}
}
However, my User class doesn't subclass NSObject so it's not Key Value Compliant. I get unrecognized selector 'valueForKey'
whenever I try this. Is there a way I can make a custom class Key Value Compliant without subclassing NSObject? Or perhaps a way to use NSPredicate without having to use KVC.
Thanks
Aren't you making this awfully unnecessarily hard for yourself? Why not just use what Swift gives you - filter
, hasPrefix
, and so on? I don't know what a User actually is, but let's pretend it's something like this:
struct User {
let username : String = ""
}
And let's pretend that users
is like this:
let users = [User(username:"Matt Neuburg"), User(username:"Dudley Doright")]
Then what you seem to be trying to do is trivial in Swift:
let word = "Matt"
let users2 = users.filter {$0.username.hasPrefix(word)}