搜索返回对象的对象数组与属性值匹配

时间:2014-02-13 05:45:58

标签: ios iphone objective-c xml nspredicate

我有一个对象数组(240个对象)准确,所有对象都有一个名为“round”的属性,我希望有一个第二个数组,如果round == x,我可以存储对象。

x是我决定的数字。

-(void) parseXMLFixtures
{

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"somethinggoeshereiwthanapikey"]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];

    NSMutableArray *items = [xml objectForKey:@"Match"];

    NSMutableArray *newFixtureObjectArray = [[NSMutableArray alloc] init];

    for (NSDictionary *dict in items) {
        FixturesObject *myFixtures = [FixturesObject fixtureFromXMLDictionary:dict];
        [newFixtureObjectArray addObject:myFixtures];
    }

    NSNull *nullValue = [NSNull null];

    [newFixtureObjectArray insertObject:nullValue atIndex:0];
    [newFixtureObjectArray insertObject:nullValue atIndex:1];

    [self setTableDataFixtures:newFixtureObjectArray];
}

任何人都知道我怎么能做到这一点?

//////

NSNumber *myRound = [NSNumber numberWithInt:11];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"round = %@", myRound];
NSArray *filteredArray = [_roundParser filteredArrayUsingPredicate:predicate];

NSLog(@" roundParser %@", filteredArray);



2014-02-13 01:00:18.383 [6864:70b]  roundParser (
)
2014-02-13 01:00:18.394 [6864:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101940795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016a3991 objc_exception_throw + 43
    2   CoreFoundation                      0x00000001018f902f -[__NSArrayI objectAtIndex:] + 175
    3  Sagres Companion           0x0000000100019ddf -[FixturesController tableView:cellForRowAtIndexPath:] + 175

2 个答案:

答案 0 :(得分:0)

试试这个,

int num = 11;
NSString *yourX = [NSString stringWithFormat:@"%d", num];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"round = %@", yourX];
NSArray *filteredArray = [objectArray filteredArrayUsingPredicate:predicate];

yourX是您需要查找的号码的NSString

答案 1 :(得分:0)

使用谓词它会应用搜索你的数组:

NSPredicate *sPredicate =
    [NSPredicate predicateWithFormat:@"SELF.round like '%@'",yourX];
[array filterUsingPredicate:sPredicate];
相关问题