iOS NSPredicate 不起作用? [英] iOS NSPredicate not working?

查看:67
本文介绍了iOS NSPredicate 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的谓词函数,如下所示:

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;

所有这些代码都存在于我原来的应用程序和工作中.它们在方法 - (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath

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

即使 cellText 确实匹配另一个应用程序中 firstLangEx 中的一个字符串,totalSentences 也不会填充任何字符.

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

什么可能导致这种情况?

What could cause this?

在 NSLog 中

totalSentences 始终不显示任何内容,这与原始应用中始终显示至少一行的输出相反.

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

firstLangEx 总是从文本文件加载正确的内容(在两个应用程序中,文件是相同的)

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

cellText 总是显示至少一个字符串.(我在两个应用中测试了相同的字符串.)

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

所以我不知道为什么 totalSentences 总是空的,因为它应该填充至少一个匹配所有 cellText 字符串的字符串.

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.

我还以编程方式向 totalSentences 添加了一些字符串,并且可变数组运行良好.知道为什么会这样吗?

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

我已经按照 yuji 的建议进行了测试:

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);

结果总是Bool ok 0".

The outcome is always "Bool ok 0".

cellText 是Babylonian",而 firstLangEx 中的一行确实包含以下内容:[E:古代巴比伦人可能写的方式.]".

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

那我做错了什么?

我用这个代码

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

- (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]);

总是显示 cellText 总是有一个额外的字符,即使我的数组中没有空格或额外的字符来加载我的表视图.例如,Babylonian"是10个字符,日志在原始应用中显示这个数量,但在另一个应用中显示11个字符,即使两个应用使用相同的文件进行表格视图.

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?

推荐答案

由于我的问题没有答案,我会在找到解决方案后回答.如果yuji因为他的贡献而被奖励,那就太好了.

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.

我通过将提供表格视图的文件的内容复制到具有其他名称的新文件中来解决它,突然所有单元格的额外不可见字符消失了.这是常见问题吗?反正现在解决了,耶!感谢 yuji.

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.

这篇关于iOS NSPredicate 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆