Take the 2-minute tour ×

I have a data like that having comma separated (sample dict below)

NSMutableArray *sessions = [[NSMutableArray alloc] init];    
demodata * sess = [[demodata alloc] init];  --->nsobject    
[sessions addObject :sess];
[sess release];   

demodata * sess1 = [[demodata alloc] init];  
[sessions addObject :sess1];          
[sess1 release];  

I want to display data of id having 1(depends on dynamic) only;

For Example: my tableview has a list of event every event has one id. for eg if I select id=1 i want to display the two objects(1 having 1st and 2nd object) mentioned above.if i select id=5 i want to display the second objects only(5 having 2nd object only) mentioned above.

.(I tried nspredicate and reading every index ,split by comma).Is there any easy way to filter

share|improve this question
It's not clear what you want. You should add some example. –  Max Jan 2 '12 at 14:05
@ Max check my edited ques –  Rams Jan 2 '12 at 14:23
and filter by what information? –  vikingosegundo Jan 2 '12 at 14:32
@vikingosegundo filter by id (Every id has a list of data) –  Rams Jan 2 '12 at 14:36
How do you want to sort the id fields? I mean sorted data should lie in some relation (for example "1,2,3" > "1,5"). How do you compare the keys? –  Max Jan 2 '12 at 14:46
show 3 more comments

3 Answers

id is an Objective-C reserved word for a pointer to an object of unknown type. I'd advise against using it as a property of a custom class. Even if it technically works, which it might not, it will be confusing.

I'm still unsure what you want, but casting to the NSString* it is might help.

((NSString*) ((demodata*)[sessions objectAtIndex:0]).id) //do something with this NSString*

Rather than trying to parse the NSString for commas, perhaps make the property an NSSArray* that holds each number as a separate index (wrap them in an instance of NSNumber first).

share|improve this answer
add comment

If you want dynamic, then use predicateWithBlock. If you want to filter a collection of demodata objects whose id contains 1, then your NSPredicate should look like this:

NSPredicate *filterWhereIdContains1 = [NSPredicate predicateWithBlock: ^BOOL(id evaluatedObject, NSDictionary *bindings){
           demodata *sess = (demodata*)evaluatedObject;
           NSArray *splitIds = [sess.id componentsSeparatedByString:@","];
           return [splitIds containsObject:@"1"];
share|improve this answer
add comment

As you did only give very vague informations about the requirements, I will show a very generic approach I am using with blocks:

write a category on array:

-(NSArray *)filter:(BOOL (^)(id))filterBlock
    NSMutableArray *filteredArray = [NSMutableArray array]; 
    for (id element in self) 
        if (filterBlock(element))   
            [filteredArray addObject:element]; 
    return [NSArray arrayWithArray:filteredArray]; 

Now you have the freedom to create a specific block matching your needs:

array = [array filter:^BOOL(id element) {
    DemoData *demoData =(DemoData *)element
    if([[demoData.sessionID componentsSeparatedByString:@","] isEqualToString:@"1"]){
        return YES
    return NO;

This would filter all sessionIDs starting with '1'.

response to edit
You should do it the other way around. You should have a NSDictionray, that uses NSNumber as keys and holds NSArrays with the objects. Than you just can ask NSArray *array = [dict objectForKey:[NSNumber numberWithInt:5]]. Now array holds all objets connected to the id (that you really should name differently).

share|improve this answer
check my edited ques –  Rams Jan 3 '12 at 4:45
see my response –  vikingosegundo Jan 3 '12 at 12:05
add comment

Your Answer


