NSPredicate类的例子程序

#import <Foundation/Foundation.h>

// 注意:下面的代码对于ARC并没有注意,可能是错误的。
// 这些代码只是为了测试NSPredicate的用法

// 嵌套在ClassA内部的类,测试通过键路径过滤
@interface InnerClass: NSObject
{
	NSString *name;
}
@property (readwrite, copy) NSString *name;
@end

@implementation InnerClass
@synthesize name;
-(id) init: (NSString *) aName
{
	if (self = [super init])
	{
		self.name = aName;
	}
	return self;
}

-(NSString *) description
{
	NSString *desc;
    desc = [NSString stringWithFormat: @"name=%@", name];
	return desc;
}
@end

@interface ClassA: NSObject
{
	// 只定义标量类型,最简单的
	int size;
	// 嵌套一个类
	InnerClass *ic;
}
@property (readwrite, assign) int size;
@property (readwrite, retain) InnerClass *ic;
@end

@implementation ClassA
@synthesize size;
@synthesize ic;
// 覆盖init方法,便于对size初始化
-(id) init: (NSNumber *) aSize ic: (InnerClass *) aIc
{
	if (self = [super init])
	{
		self.size = [aSize intValue];
		self.ic = aIc;
	}
	return self;
}
// 覆盖description方法,便于%@输出具体的值
-(NSString *) description
{
	NSString *desc;
    desc = [NSString stringWithFormat: @"size=%i name=%@", self.size, self.ic.name];
	return desc;
}
@end

NSMutableArray *createMutableArray()
{
    int i;
    NSMutableArray *m_array = [NSMutableArray arrayWithCapacity: 20];
    for (i = 0; i < 20; ++i)
    {
    	[m_array addObject: 
    		[[ClassA alloc]init: 
    		[NSNumber numberWithInt: i]
    		ic: [[InnerClass alloc]init: [NSString stringWithFormat: @"0x%08x", i]]]
    	];
    }
    return m_array;
}

int main(const int arg, const char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    // 普通的初始化
    NSMutableArray *m_array;
    NSPredicate *predicate;
    id t;
    m_array = createMutableArray();
    // 以下2种方法实现过滤
    predicate = [NSPredicate predicateWithFormat: @"size>1 && size<4"];
    // 笨办法
    NSLog(@"predicateFormat=%@", [predicate predicateFormat]);
    for (t in [m_array objectEnumerator])
    {
        if ([predicate evaluateWithObject: t] == TRUE)
       	{
       		NSLog(@"%@", t);
       	}
    }

    // 下面是个使用了键路径的例子
    m_array = createMutableArray();
    predicate = [NSPredicate predicateWithFormat: @"ic.name='6'"];
    // 笨办法
    NSLog(@"predicateFormat=%@", [predicate predicateFormat]);
    for (t in [m_array objectEnumerator])
    {
    	if ([predicate evaluateWithObject: t] == TRUE)
    	{
    		NSLog(@"%@", t);
    	}
    }

    // 其实,NSMutableArray已经实现了NSPredicate类别
    // 实现了方法- (void) filterUsingPredicate: (NSPredicate *)predicate;
    m_array = createMutableArray();
    predicate = [NSPredicate predicateWithFormat: @"ic.name<'0x00000003'"];
    [m_array filterUsingPredicate: predicate];
    NSLog(@"predicateFormat=%@ array=%@", [predicate predicateFormat], m_array);

	[pool drain];
	return 0;
}

以下是windows下输出的内容

C:\GNUstep\msys\1.0\home\pro\cpro>cpro.exe
2015-02-05 14:17:49.117 cpro[2508] predicateFormat=size >= 1 AND size < 4 这里可以用组合条件
2015-02-05 14:17:49.117 cpro[2508] size=2 name=0x00000002
2015-02-05 14:17:49.117 cpro[2508] size=3 name=0x00000003
2015-02-05 14:17:49.117 cpro[2508] predicateFormat=ic.name = 6 注意:这个查询没有结果,因为name被格式化成0x0000000?的形式了
2015-02-05 14:17:49.133 cpro[2508] predicateFormat=ic.name < 0x00000003 array=("size=0 name=0x00000000", "size=1 name=0x00000001", "size=2 name=0x00000002")




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值