2014-02-13 57 views
2

我有一个精确的对象数组(240个对象),所有对象都有一个名为“圆”的属性,我想有一个第二个数组,我可以存储对象如果轮== 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 

回答

0

试试这个,

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

yourX是,你需要找到一些NSString

+0

阵列回来空 – user3295409

+0

你肯定有在'objectArray'匹配对象以'轮== X'? – Akhilrajtr

+0

是的,我使用了10,数组有1到30的每个数字有8个对象。 – user3295409

0

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

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