方便实现逻辑功能的代码

通常我们可以使用NSPredicate来进行逻辑计算,不过一般来说代码都很长。

比如考虑,要实现如下逻辑:(A or B) but not (A and B) 需要用很长的一堆代码来实现:

  1. NSPredicate  *xor  =  [ NSCompoundPredicate  andPredicateWithSubpredicates :
  2.     [ NSArray  arrayWithObjects :
  3.        [ NSCompoundPredicate  orPredicateWithSubpredicates :
  4.           [ NSArray  arrayWithObjects : a, b,  nil ]
  5.        ],
  6.        [ NSCompoundPredicate  notPredicateWithSubpredicate :
  7.           [ NSCompoundPredicate  andPredicateWithSubpredicates :
  8.              [ NSArray  arrayWithObjects : a, b,  nil ]
  9.           ]
  10.        ],
  11.        nil
  12.     ]
  13. ];

不过如果用了本文介绍的方法,就会简单地变成下面的代码:

  1. NSPredicate  *xor  =  [ [a or : b ] and :  [ [a and : b ] not ] ];

同样地,你还可以使用xor运算: 

  1. NSPredicate  *xor  =  [a xor : b ];

本文介绍的代码是一个Foundation Kit的NSPredicate的扩展,作者是Jonathan Grynspan, 你可以在这里下载到这个代码。



http://www.cocoachina.com/b/?p=217#more-217

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值