-1

我正在尝试归档此对象的数组:

我的对象.h

@interface MyObject : NSObject

@property (assign) int id_object;
@property (nonatomic, strong) NSString *name;

@end

因此,例如在我的 firstArray 中,我有一些 MyObject:

MyObject *ob1 = [[MyObject alloc] init];
[ob1 setId_Object:1;
[ob1 setName:@"Carl"];

MyObject *ob2 = [[MyObject alloc] init];
[ob2 setId_Object:2;
[ob2 setName:@"Carl"];

MyObject *ob3 = [[MyObject alloc] init];
[ob3 setId_Object:3;
[ob3 setName:@"Carl"];

NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];

然后我想用 NSPredicate 和一个 NSNumber 数组过滤这个数组,像这样:

NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];

我试过这个:

 NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"id_object IN %@",secondArray]];

但是filteredObject是空的,我该怎么办?

4

3 回答 3

0

一旦检查这个,

 NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.Carl contains %@ OR self.Carl contains %@ ",@"1",@"2"]];

前任:-

 NSArray*  questionSections=[[NSArray alloc] initWithObjects:[[NSDictionary alloc] initWithObjectsAndKeys:
@"List 3 rooms in your house?",@"name",@"301q.png",@"questionpicture",nil],[[NSDictionary alloc] initWithObjectsAndKeys:@"Name a commonbird?",@"name",@"202q.png",@"questionpicture",nil], nil];


 NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.questionpicture contains %@ OR self.questionpicture contains %@ ",@"2",@"3"]];
    NSLog(@"%@",filteredObject);

运单:--

(
        {
        name = "List 3 rooms in your house?";
        questionpicture = "301q.png";
    },
        {
        name = "Name a common bird?";
        questionpicture = "202q.png";
    }
)
于 2013-07-24T11:24:36.720 回答
0

尝试更换

@property (nonatomic, copy) NSNumber* id_object;
@property (nonatomic, copy) NSString *name;

代替

@property (assign) int id_object;
@property (nonatomic, strong) NSString *name;
于 2013-07-24T11:26:41.853 回答
0

似乎是你的测试用例是错误的。我认为您的 setId_object 不会像您在使用 NSNumber 调用它时所做的那样;

[ob1 setId_object:[NSNumber numberWithInt:1]];
NSLog(@"%d", ob1.id_object);

> 391

[ob1 setId_object:1];
NSLog(@"%d", ob1.id_object);

> 1

替换初始化使您的测试用例运行正常。

编辑:我的测试用例,剪切粘贴;

MyObject *ob1 = [[MyObject alloc] init]; ob1.id_object = 1; [ob1 setName:@"Carl"];
MyObject *ob2 = [[MyObject alloc] init]; ob2.id_object = 2; [ob2 setName:@"Carl"];
MyObject *ob3 = [[MyObject alloc] init]; ob3.id_object = 3; [ob3 setName:@"Carl"];

NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];
NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];

NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"id_object IN %@",secondArray]];

NSLog(@"%@", filteredObject);

>>> <MyObject: 0x100110350> {1,"Carl"}
>>> <MyObject: 0x1001104b0> {2,"Carl"}
于 2013-07-24T11:27:41.797 回答