iOS NSPredicate not working?

2019-07-29 21:38发布

I have a simple predicate function that follows as:

[totalSentences addObjectsFromArray:[firstLangEx filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF contains[c] %@)", cellText]]];

This line works in one of my apps, however it does not work when I copied this into another app and tried testing this.

totalSentences is a global NSMutableArray

firstLangEx is a local NSArray filled with several lines

cellText is  NSString *cellText = cell.textLabel.text;

All these codes exist in my original app and work. They are implemented in the method - (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Even cellText does match one string in firstLangEx in the another app, totalSentences is never filled with any character.

What could cause this?


EDIT:

In NSLog

totalSentences always shows no content, contrary to the output in the original app which always shows at least one line.

firstLangEx does always have correct content loaded from a text file (In both apps the files are same)

cellText does always shows at least one string. (I test same strings in both apps.)

So I have no idea why totalSentences is always empty, when it should be filled with at least one string which matches all cellText strings.


EDIT:

I have also added some strings to totalSentences programmatically and the mutable array works well. Any idea why this happens?


EDIT:

I have tested as yuji suggested:

BOOL ok;

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", cellText];

ok = [predicate evaluateWithObject: firstLangEx];

NSLog (@"Bool ok %d", ok);

The outcome is always "Bool ok 0".

cellText was "Babylonian" and one line from firstLangEx does contain this: "[E: The way an ancient Babylonian might have written it.]".

So what did I do wrongly?


EDIT:

I use this code

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;

in

- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath

in order to extract the string to match a string from my array.

However, it never gets matched, as

NSLog (@"cellText length %d", [cellText length]);

always show that cellText has always one extra character, even there is no white space or extra character in my array that my table view is loaded from. For example, "Babylonian" is 10 characters and the log showed this amount in the original app, but it showed 11 characters in the other app, even both apps use same files to table view.

Why does this happen?

1条回答
做自己的国王
2楼-- · 2019-07-29 22:16

Since there is no answer to my question, I will answer as I have found the solution. It would be nice if yuji would be awarded for his contribution.

I solved it by copying the content of the files that feed table view into new files with other names, and suddenly the extra invisible character for all cells disappeared. Is this common problem? Anyway, it is solved now, yay! Thanks to yuji.

查看更多
登录 后发表回答