过滤nsmutablearray问题

时间:2011-12-12 07:18:25

标签: iphone objective-c cocoa-touch

@interface demodata : NSObject             
{  
   NSString *Day;     
   NSString *content;   
   @property (nonatomic, retain) NSString *Day;  
   @property (nonatomic, retain) NSString *content;  
}  
-------

Test.m file--  

NSMutableArray *sessions = [[NSMutableArray alloc] init];  
demodata * sess = [[demodata alloc] init];  
sess.Day=@"Monday";  
sess.content=@"HI";  
 [sessions addObject :sess];  
[sess release];  


demodata * sess1 = [[demodata alloc] init];  
sess1.Day=@"Tuesday";  
sess1.content=@"Bye";  
 [sessions addObject :sess1];  
[sess1 release]; 

我试过

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Day == %@", @"Monday"];  
NSArray *filteredArray = [sessions filteredArrayUsingPredicate:predicate];  

我的数组对象是类(nsobject)..
它不起作用......

如何按天过滤数组(会话)..

2 个答案:

答案 0 :(得分:1)

您可以使用NSArray s -filteredArrayUsingPredicate:并传递NSPredicate来描述您的需求。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Day = %@", @"Tuesday"];
NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];

答案 1 :(得分:0)

此代码工作正常..问题是数组对象中有一些空格