通过以编程方式设置键来动态创建NSPredicate [英] Creating NSPredicate dynamically by setting the key programmatically

查看:129
本文介绍了通过以编程方式设置键来动态创建NSPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下代码段之前的代码段不工作,而不是后者?

Why does the former of following snippets work while not the latter ?

代码段1

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(coin_unique == %@)", [NSNumber numberWithInt:species]];

程式码片段2

// Does NOT Work
NSString *predicateText = @"coin_unique";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ == %@)", predicateText, [NSNumber numberWithInt:species]];

我必须动态创建谓词,取决于我的方法中接收的参数。

I have to dynamically create predicate depending upon the argument received in my method.

推荐答案

coin_unique 是一个键,所以需要%K 格式说明符:

coin_unique is a key, so it needs the %K format specifier:

NSString *predicateText = @"coin_unique";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K == %@)", predicateText, [NSNumber numberWithInt:species]];

格式语法描述得相当不错这里

The format syntax is described quite well here.

这篇关于通过以编程方式设置键来动态创建NSPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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