I am attempting to use NSArrays and NSPredicates to filter certain cells of a UITableView. I have a TableViewController class called FilterTable that is made up of FilterCell, which is a subclassed UITableViewCell. The data in a filter cell comes from a UserProfile, which has a property "major". When the user clicks a button, I want the table to show only the cells that correspond to users that have Computer Science as their major. I made an NSPredicate to filter the array of UserProfile that I use to populate my table, but when I do this I get the error
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key major.'
However, as I said major is a property of UserProfile.
Other fixes to keyvalue compliancy issues suggest that outlets are not connected correctly in interface builder. However, UserProfile is not directly shown on FilterTable and is therefore not connected through IB, nor do I want it to be. Instead FilterTable has an array of UserProfiles which it uses to populate FilterCells. I also tried linking the FilterCell to FilterTable and then I could access the label corresponding to major, but that gave me an error that you cannot make an outlet to repeating content.
// FROM FilterTable:
...
let predicate = NSPredicate(format: "major = 'Computer Science'")
filteredProfiles = (userProfilesArray as NSArray).filtered(using: predicate) as NSArray
...
// FROM UserProfile
class UserProfileViewController: UIViewController {
var major: String!
...
userProfilesArray.filter{$0.major == "Computer Science"}
? Don't useNSArray
in Swift anyway. You throw away the type information. But according to the code themajor
property is part of a contoller but it should be a part of the model (the type ofuserProfilesArray
). – vadian