cocoa – 重用NSPredicate用于新的变量替换

cocoa – 重用NSPredicate用于新的变量替换,第1张

概述我可以重用NSPredicate来替换新的变量吗?我的NSPredicate相当简单: NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"id == %@",userID]; userID是在运行时生成的,具有不同的值,现在对于我需要创建NSPredicate的每个userID.那么我可以重用我的NSPredicate吗 我可以重用nspredicate来替换新的变量吗?我的nspredicate相当简单:

nspredicate *userPredicate = [nspredicate predicateWithFormat:@"ID == %@",userID];

userID是在运行时生成的,具有不同的值,现在对于我需要创建nspredicate的每个userID.那么我可以重用我的nspredicate吗?

解决方法 是的,您可以使用变量执行此 *** 作:

nspredicate *userPredicate = [nspredicate predicateWithFormat:@"ID == $user"];

将该谓词保存在某个地方,然后在需要实际开始过滤内容时执行此 *** 作:

NSDictionary *sub = [NSDictionary dictionaryWithObject:user forKey:@"user"];nspredicate *filter = [userPredicate predicateWithSubstitutionVariables:sub];

这是重用谓词的一种更有效的方法,因为解析格式字符串(可能相当昂贵)只发生一次.

总结

以上是内存溢出为你收集整理的cocoa – 重用NSPredicate用于新的变量替换全部内容,希望文章能够帮你解决cocoa – 重用NSPredicate用于新的变量替换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1015385.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存