Friday, April 29, 2011

Xcode4/CoraData: Using LIKE to search SQLite Table - "String starts with"

Problem


I recently had a task where a UITableView should suggest autocomplete for a word I started to type into a UISearchBar/Search Display Controller.

From the top of my head I came up with the SQL query that I wanted to use:

SELECT string FROM table WHERE string LIKE 'S%';

This query should return all the strings that start with capital S.

Solution


For a NSPredicate the query has to look like this:

NSString *query = [NSString stringWithFormat:@"String LIKE '%@%%'", startOfString];

Most important here is that the percent sign needs to be escaped with another percent sign (%%).

0 comments:

Post a Comment