1

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)

10
  • why _toField.textField.text? should be _toField.text Commented Nov 5, 2013 at 20:10
  • 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/… Commented Nov 5, 2013 at 20:11
  • have you tried using SELF.cLastName? Commented Nov 5, 2013 at 20:13
  • Hm, I'm not really sure, what do you mean. Use it in my predicate? Or where? Commented Nov 5, 2013 at 20:15
  • 2
    @Yanchi: Can you show the output of [fieldText dataUsingEncoding:NSUTF8StringEncoding], to check was is really in the string? Commented Nov 5, 2013 at 20:21

1 Answer 1

1

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]];
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Martin, it's good answer, I'm going to accept it, however problem still remains... string "Abc" gets converted to <e2808b41 6263> so I probably have to use different class than JSTokenField.
@Yanchi: The above code should work even with <e2808b41 6263>.
NSString *fieldText =_toField.textField.text; NSLog(@"Before %@",[fieldText dataUsingEncoding:NSUTF8StringEncoding]); [fieldText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"After %@",[fieldText dataUsingEncoding:NSUTF8StringEncoding]);
Sorry for messy input... I tried it, however, it gives me same result... Maybe I'm doing something wrong :)
@Yanchi: Do you assign the trimmed result to fieldText? Your code looks different from my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.