方便实现逻辑功能的代码
2011-10-04 23:01
260 查看
通常我们可以使用NSPredicate来进行逻辑计算,不过一般来说代码都很长。
比如考虑,要实现如下逻辑:(A or B) but not (A and B) 需要用很长的一堆代码来实现:
NSPredicate *xor = [NSCompoundPredicate andPredicateWithSubpredicates:
[NSArray arrayWithObjects:
[NSCompoundPredicate orPredicateWithSubpredicates:
[NSArray arrayWithObjects: a, b, nil]
],
[NSCompoundPredicate notPredicateWithSubpredicate:
[NSCompoundPredicate andPredicateWithSubpredicates:
[NSArray arrayWithObjects: a, b, nil]
]
],
nil
]
];
不过如果用了本文介绍的方法,就会简单地变成下面的代码:
NSPredicate *xor = [[a or: b] and: [[a
and: b] not]];
同样地,你还可以使用xor运算:
NSPredicate *xor = [a xor: b];
本文介绍的代码是一个Foundation Kit的NSPredicate的扩展,作者是Jonathan Grynspan, 你可以在这里下载到这个代码。
http://www.cocoachina.com/b/?p=217#more-217
比如考虑,要实现如下逻辑:(A or B) but not (A and B) 需要用很长的一堆代码来实现:
NSPredicate *xor = [NSCompoundPredicate andPredicateWithSubpredicates:
[NSArray arrayWithObjects:
[NSCompoundPredicate orPredicateWithSubpredicates:
[NSArray arrayWithObjects: a, b, nil]
],
[NSCompoundPredicate notPredicateWithSubpredicate:
[NSCompoundPredicate andPredicateWithSubpredicates:
[NSArray arrayWithObjects: a, b, nil]
]
],
nil
]
];
不过如果用了本文介绍的方法,就会简单地变成下面的代码:
NSPredicate *xor = [[a or: b] and: [[a
and: b] not]];
同样地,你还可以使用xor运算:
NSPredicate *xor = [a xor: b];
本文介绍的代码是一个Foundation Kit的NSPredicate的扩展,作者是Jonathan Grynspan, 你可以在这里下载到这个代码。
http://www.cocoachina.com/b/?p=217#more-217
相关文章推荐
- 方便实现逻辑功能的代码
- ViewPager实现自动翻页功能 --转载出处找不到了,根据自己的理解写个随笔方便以后的记忆以及代码的共享,感谢给我启发的那位高手--第一次写博客哈
- Java实现分页功能 代码很粗糙,都是根据自己所需去定义逻辑功能的。
- Eclipse中实现JS代码提示功能
- Android应用程序实现自动更新功能4_实现的整体代码
- android实现倒计时功能代码
- c# .net中如何实现图片防盗链功能的代码实例
- java实现下载功能Controller层代码
- iPhone 应用中实现拨打电话功能的代码
- JS实现仿百度输入框自动匹配功能的示例代码
- PHP实现留言板功能的详细代码
- jsp实现连接数据库、查询、插入、修改功能的javaBean代码
- Ajax实现评论中顶和踩功能的实例代码
- Android应用自动更新功能的代码实现
- android代码实现搜索机身应用的功能
- 使用Visual Studio(VS)开发Qt程序代码提示功能的实现(转)
- 利用jQuery简单实现产品展示图片左右滚动功能(示例代码)
- 关于AdoHelper的一个方便之处的代码实现细节
- php视频拍照上传头像功能实现代码分享
- jQuery实现倒计时按钮功能代码分享