1
votes

My app utilizes core data. I have data source (NSMutableArray) loaded with core data records. To filter the records, I'm using textfield (JSTokenField) that has UITextField delegate in it's .m file.

When I'm filtering my search results, I'm using a simple predicate:

-(void)textFieldChanged
{
    NSString *fieldText =_toField.textField.text;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cLastName contains[cd] %@",fieldText];
    NSArray *testArr = [coreDataSource filteredArrayUsingPredicate:predicate]; //coreDataSource is my MutableArray with all core data records
    NSLog(@"%@",predicate);
    NSLog(@"Testarr %@",testArr);
}

NSLog returns empty array :

2013-11-05 20:53:36.765 MyApp[3045:60b] cLastName CONTAINS[cd] "​a"
2013-11-05 20:53:36.766 Myapp[3045:60b] Testarr (
)

However, when I change(hard code) my predicate string like this: NSString *fieldText = @"a"; in my code, I get valid results.

2013-11-05 21:00:34.917 MyApp[3054:60b] cLastName CONTAINS[cd] "a"
2013-11-05 21:00:34.929 MyApp[3054:60b] Testarr (
 "<MyUser: 0x1566fed0> (entity: MyUser; id: 0x156dbc30 <x-coredata://33798AE4-9768-4A2F-A0E8-C094F32972AD/MyUser/p5565> ; data: {\n    activities = \"<relationship fault: 0x1551c540 'activities'>\";\n etc...

I'm really lost, as why doesn't it work with subclassed textfield...

btw, coreDataSource array is array of custom objects, with various properties (cLastName is one of them)

1
why _toField.textField.text? should be _toField.text - ManicMonkOnMac
well _toField is class that has several properties in it. Label, textfield, etc.. So I have to access it's textfield property. You can check it out here github.com/FivesquareSoftware/JSTokenField/blob/master/… - Yanchi
have you tried using SELF.cLastName? - ManicMonkOnMac
Hm, I'm not really sure, what do you mean. Use it in my predicate? Or where? - Yanchi
@Yanchi: Can you show the output of [fieldText dataUsingEncoding:NSUTF8StringEncoding], to check was is really in the string? - Martin R

1 Answers

1
votes

As you said in a comment, fieldText converted to UTF-8 is e2 80 8b 41.

UTF-8 e2 80 8b is Unicode U+200B (ZERO WIDTH SPACE).

I cannot tell you where that special space character comes from, but if it occurs only at the start or the end of the string, you could get rid of it with

fieldText = [fieldText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];