0

I have a code like below.

Ticket *ticket1 = [[Ticket alloc] initWithName:@"INC1234" group:@"PIMS-EU-COG" type:@"Incident" status:@"Active" createdOn:@"20-Oct-2013" createdBy:@"Dinesh" completionDate:@"23-Oct-2013" notes:@"1. All works coreectly 2. New variables are created.."];
Ticket *ticket2 = [[Ticket alloc] initWithName:@"PRJ2342" group:@"PIMS-EU-COG" type:@"Project" status:@"Closed" createdOn:@"20-Oct-2013" createdBy:@"Dinesh" completionDate:@"23-Oct-2013" notes:@"1. All works coreectly 2. New variables are created.."];
Ticket *ticket3 = [[Ticket alloc] initWithName:@"ENC1234" group:@"PIMS-EU-COG" type:@"Enhancement" status:@"Awaiting vendor" createdOn:@"20-Oct-2013" createdBy:@"Dinesh" completionDate:@"23-Oct-2013" notes:@"1. All works coreectly 2. New variables are created.."];
Ticket *ticket4 = [[Ticket alloc] initWithName:@"INC2345" group:@"CRM-EU-COG" type:@"Incident" status:@"Active" createdOn:@"20-Oct-2013" createdBy:@"Alex" completionDate:@"23-Oct-2013" notes:@"1. All works coreectly 2. New variables are created.."];
Ticket *ticket5 = [[Ticket alloc] initWithName:@"PRJ2345" group:@"CRM-EU-COG" type:@"Project" status:@"Pending" createdOn:@"20-Oct-2013" createdBy:@"Alex" completionDate:@"23-Oct-2013" notes:@"1. All works coreectly 2. New variables are created.."];
Ticket *ticket6 = [[Ticket alloc] initWithName:@"ENC2345"group:@"CRM-EU-COG" type:@"Enhancement" status:@"Active" createdOn:@"20-Oct-2013" createdBy:@"Alex" completionDate:@"23-Oct-2013" notes:@"1. All works coreectly 2. New variables are created.."];

self.TicketsFinal= [NSMutableArray arrayWithObjects:ticket1,ticket2,ticket3, nil];
/*
if (groupFinal == @"PIMS-EU-COG") {

    self.TicketsFinal= [NSMutableArray arrayWithObjects:ticket1,ticket2,ticket3, nil];
}else if (groupFinal == @"CRM-EU-COG")
{
    self.TicketsFinal= [NSMutableArray arrayWithObjects:ticket4,ticket5,ticket6,ticket7,ticket8,ticket9, nil];
}*/

I have 3 parameters:

  1. GroupFinal
  2. TypeFinal
  3. StatusFinal.

groupsFinal will hold the group (ex:PIMS-EU-COG) details. typeFinal will hold what type of ticket (ex: Incident) and statusFinal will hold status of the ticket (ex: Active). These 3 parameters are obtained from previous view.

I have a array called as self.ticketsFinal which should hold tickets based on selected criteria alone. I tried hardcoding the group (last commented part) and navigating, which is not the correct way.

Ex: When groupFinal, TypeFinal, statusFinal have values as PIMS-EU-COG, Incident, Active, then in this case the self.TiketsFinal should hold only Ticket1 (which meets the criteria).

When an All is selected in typeFinal and statusFinal all the tickets in the group should be updated in array.

Can anyone please help. You help is much appreciated.

1 Answer 1

2

You can probably use NSPredicate for this purpose:

[self.TicketsFinal filteredArrayUsingPredicate:
    [NSPredicate predicateWithFormat:@"group = %@", @"PIMS-EU-COG"]];

This would return a new array containing only the objects for which the group is equal to 'PIMS-EU-COG'.

You can see a guide to using NSPredicate here.

Also, if you're using this method, your array doesn't need to be mutable. If you really want to discard the items that don't match, you can do

[self.TicketsFinal filterUsingPredicate:
    [NSPredicate predicateWithFormat:@"group = %@", @"PIMS-EU-COG"]];

In your case, you have multiple parameters that you want to use to filter down the set of objects. You can use NSCompoundPredicate for this:

NSPredicate *groupPredicate = [NSPredicate 
     predicateWithFormat:@"group = %@", @"PIMS-EU-COG"];

NSPredicate *typePredicate = [NSPredicate 
     predicateWithFormat:@"type = %@", @"Incident"];

NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate 
     andPredicateWithSubpredicates:@[groupPredicate, typePredicate]];

NSArray *result = [self.TicketsFinal filteredArrayUsingPredicate:compoundPredicate];
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Mat..Thanks for you reply. It is very good stuff to learn. But my requirement little different. I have the tickets as NSObjects. How can i dynamically assign the object to the Array. i tried the code you provided but it returns empty value.
Sorry Mat..Got it right now. !! Thanks a lot for your help. Much appreciated.!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.