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用于新的变量替换所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)