Mehmet Ince
Mehmet Ince

Reputation: 4189

Array filtering using NSPredicate in objective-c gives error

I have an array within objects like below;

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

TableStructure *structure = [[TableStructure alloc] init];
                    structure.TableName =@"Client";
                    structure.Type =@"U";
                    structure.ColumnName =@"Id";
                    structure.DataType =@"int";
                    structure.Length = 2;

TableStructure *structure2 = [[TableStructure alloc] init];
                    structure2.TableName =@"Client";
                    structure2.Type =@"U";
                    structure2.ColumnName =@"Name";
                    structure2.DataType =@"text";
                    structure2.Length = 20;

[tableStructure addObject:structure];
[tableStructure addObject:structure2];


 NSString *typeFilter = @"U";
 NSString *nameFilter = @"sysname";
 NSString *tableNameFilter = @"Sync";

 NSPredicate *pred = [NSPredicate predicateWithFormat:@"Type == %@ and DataType != %@ and TableName != %@)", typeFilter, nameFilter, tableNameFilter];

 NSArray *filteredArray = [tableStructure filteredArrayUsingPredicate:pred];

When the debugger came to NSPredicete *pred... line, it gives 'Unable to parse the format string "Type == %@ and DataType != %@ and TableName != %@)"' error.

How can I solve this?

Upvotes: 0

Views: 210

Answers (3)

Lithu T.V
Lithu T.V

Reputation: 20021

Try this,No need of )

 NSPredicate *pred = [NSPredicate predicateWithFormat:@"Type == %@ and DataType != %@ and TableName != %@", typeFilter, nameFilter, tableNameFilter];

Upvotes: 1

tilo
tilo

Reputation: 14169

You simply have an orphaned ")", try this: [NSPredicate predicateWithFormat:@"Type == %@ and DataType != %@ and TableName != %@", typeFilter, nameFilter, tableNameFilter]; `

Upvotes: 1

Martin R
Martin R

Reputation: 540075

You have an unbalanced closing parenthesis in the predicate string:

@"Type == %@ and DataType != %@ and TableName != %@)"
                                           HERE ---^

If you remove that it should work.

Upvotes: 1

Related Questions