Adam G
Adam G

Reputation: 1188

Using NSPredicate with NSMutableArray

I'm trying to use the input on an NSTextField along with NSPredicate to populate a NSMutableArray of like objects.

In my app I have the following simple NSMutableArray

{
        firstName = Danton;
        phoneNumbers = 5555555555;
}

Now, I am using the following code to try and filter the results as the user types (if "Dan" is in the textField, the filtered array should include the above object)

NSMutableArray *filteredArray = [[NSMutableArray alloc] init];

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"firstName CONTAINS[cd] %@", self.searchField.text];

filteredArray = [[filteredArray filteredArrayUsingPredicate:sPredicate] mutableCopy];

NSLog(@"Filtered values: %@", filteredArray);

However, I am getting an empty return on filteredArray. Can someone tell me what is wrong with my code?

Upvotes: 1

Views: 531

Answers (1)

serdaryillar
serdaryillar

Reputation: 413

  NSArray *array = @[

          @{
                  @"firstName" : @"Danton", @"phoneNumbers" : @"5555555555"
          }
  ];

  NSArray *result = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K contains[c] %@", @"firstName", @"Dan"]];

Upvotes: 0

Related Questions