iOS NSPredicate not working?

☆樱花仙子☆ 提交于 2019-12-11 09:53:07

问题


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:


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.



来源:https://stackoverflow.com/questions/9268230/ios-nspredicate-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!