I am working with sqlite database.
I retrieve text data from sqlite database into NSMutableArray named myArray.
In that myArray variable, there is a column named “info” with text data.
After i retrieve that data into NSMutableArray , i would like to convert all of text data from sqlite database into lowercase.
I wrote following code in searchBar’s textDidChange Event like that.
NSInteger counter = 0;
for(NSString *nameMe in myArray)
{
NSRange r = [[nameMe lowercaseString] rangeOfString:[searchText lowercaseString]];
if(r.location != NSNotFound)
{
if(r.location== 0)
{
[tableData addObject:nameMe];
}
}
counter++;
}
However there is a error occurring in lowercaseString.
This error is
2012-03-31 16:28:18.217 SqliteTest[1812:f803] -[MyInfoClass lowercaseString]: unrecognized selector sent to instance 0x6da9210
2012-03-31 16:28:18.276 SqliteTest[1812:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyInfoClass lowercaseString]: unrecognized selector sent to instance 0x6da9210'
I think that compiler doesn’t know how to convert object data into lowercase.
How can i solve that?
Can i convert object (NSString) data into lowercase.
If it can be,Please let me know how to do that.
Thanks you for your helping.
The problem is that objects of type MyInfoClass don’t have a method
-lowercaseString. You’re iterating over the array assuming that every object in the array is aNSString, but that’s apparently not true — there’s definitely at least one instance of MyInfoClass inmyArray. So, one way to fix the problem is to make sure that you only add strings to the array.Here’s a shorter, safer way to do what you’re doing above:
NSArray’s
-valueForKey:will send a-valueForKey:message with the key you provide to every object in the array, and collect the results in an array. Even better, if some object in the array returns nil, the resulting array will contain a NSNull object at that index. You’ll want to check for those NSNull’s when you use the array, but you won’t get an exception like you do in your code.Update: From the OP’s comment, it seems that the reason for converting the array to lowercase is to make case insensitive searching possible. It’s worth pointing out that there are better ways to accomplish that. Here are two:
Use NSString’s
-caseInsensitiveCompare:method to compare strings, if you’re doing the comparison yourself.If you’re using a predicate to search for a match, use string comparison operators (LIKE, CONTAINS, etc.) with the case-insensitive option: @”SELF like[c] %@”. You may also want the diacritic-insensitive option: @”SELF like[cd] %@”.