NSCFString countByEnumeratingWithState:objects:count: ERROR while searching NSMutableArray - iphone

I have the following situation where
I have an NSMutableArray filled with an xml file which I want to search.
When I enter something in the search field I get this Error:
-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x5b388b0
What does it means and how can I fix it??
I suppose the error is somewhere around here.
- (void)searchTableView{
searchedList = [[NSMutableArray alloc] init];
NSLog(#"new list %#", searchedList);
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in list) {
NSArray *array = [dictionary objectForKey:#"TITLE"];
[searchArray addObjectsFromArray:array];
}
for (NSString *TempArray in searchArray) {
NSRange titleResults = [TempArray rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResults.length > 0)
[searchedList addObject:TempArray];
}
[searchArray release];
searchArray = nil;
}

it means you are calling a method designed for an NSArray (countByEnumeratingWithState:objects:count on a NSString.
I don't know ifthis code is copy/paste from yours, but if so, at the end where you use [searchList addObject:TempArray] you don't have an object named searchList.
Also, work on your naming conventions. big time.

Related

How to join an NSArray output to an NSString separated with commas

I'm using the following code to try to join the array output into an NSString.
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
I would like this to output the joined string as: joined string is 55,56,57,66,88... etc... at the moment the output is:
2013-03-05 13:13:17.052 [63705:907] joinedString is 55
2013-03-05 13:13:17.056 [63705:907] joinedString is 56
2013-03-05 13:13:17.060 [63705:907] joinedString is 57
2013-03-05 13:13:17.064 [63705:907] joinedString is 66
You are probably running the join method inside a loop.
I suppose this is what you want.
NSMutableArray * array1 = [NSMutableArray array]; // create a Mutable array
for(id item in items){
[array1 addObject:[item objectForKey:#"id"]]; // Add the values to this created mutable array
}
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
You can do it as,
take for example
NSArray *array=#[#"A",#"B",#"C"];
NSString *string=[array componentsJoinedByString:#","];
NSLog(#"%#",string);
Output is :
A,B,C
What ever you are writing that one correct may be problem in [item objectForKey:#"id"] once check this one other all are fine.
NSMutableArray *array = [[NSMutableArray alloc]
initWithObjects:#"55",#"56",#"57",#"58", nil];
NSString *joinedString = [array componentsJoinedByString:#","];
NSLog(#"%#",joinedString);
I have been commenting on a couple of the answers here and found that most of the answers are just giving the code provided as the answer to solve this code, and the reason for that is because the code provided (See Provided code) works perfectly fine.
(Provide by question asker)
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
As the user hasn't provided how the item NSDictionary is created I am assuming that an NSArray has been created which contains some NSDictionaries
NSArray *array = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:#"55", #"id", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"65", #"id", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"75", #"id", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"65", #"id", nil],
nil];
The problem is with the code that hasn't been provide, because we know that item is an NSDictionary we know that [item objectForKey:#"id"] doesn't return an individual items it returns an NSArray of ids. So based on if it was an NSArray it would log something like joinedString is (55, 56, 57...)". We also know that it can't just be a string as we would also only have one value than so it would log some thing like this joinedString is 55, and again this isn't what is wanted so. the only way to get what has been provided would be to have something like this
for(NSDictionary *item in array) {
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
}
So if this is the case than the way to resolve this would be to do
NSMutableArray *array1 = [NSMutableArray array];
for(NSDictionary *item in array) {
[array1 addObject:[item objectForKey:#"id"]];
}
// Note that this doesn't need to be in a for loop `componentsJoinedByString:` only needs to run once.
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
The output of this would be (As user wants)
joinedString is 55,65,75,65
Once the question asker provides the missing code I will correct his to answer based on there code but until then I am assuming.
EDIT:
First Check [item objectForKey:#"id"] it is proper or not ??
And Then use following code :
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *commaSpStr;
commaSpStr = [array1 componentsJoinedByString:#", "];
NSLog(#"%#", commaSpStr);
You are recreating array1 everytime. Create an instance variable of array1, insert [item objectForKey:#"id"] value to it and you will see joinedString will be updated.
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (NSDictionary *item in array) {
[arr addObject:[item objectForKey:#"id"]];
}
NSString *joinedStr = [arr componentsJoinedByString:#","];

Problem with UISearchBar and NSMutableArray

I have a tableview with a search bar. I want to search my whole core data database.
I loaded everything into an array here:
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Entity" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSError *error = nil;
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
self.myArray = array;
I'm then searching using this method but getting an error:
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in myArray)
{
NSString *value = [dictionary objectForKey:#"name"];
[searchArray addObject:value];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
But when I search, I get the crash here:
MedicalCode *code = [self.copyListOfItems objectAtIndex:indexPath.row];
cell.codeLabel.text = code.description;
Error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ICD9Disease objectForKey:]: unrecognized selector sent to instance 0x1be6e0'
You are getting the error because you are asking for an object, but you're trying to set a string, in the line:
NSString *value = [dictionary objectForKey:#"name"];
Since you're getting a string, not an object, what you need to use is valueForKey:
NSString *value = [dictionary valueForKey:#"name"];
It looks like you're expecting the fetch request to return dictionaries, but it's actually returning instances of the class ICD9Disease.

Having Problems With UISearchBar in UITableViewController

I am trying to add a UISearchBar to my UITableView.
This is the method they use to search:
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:#"Countries"];
[searchArray addObjectsFromArray:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
But I don't have a dictionary, I just want to search in my array 'exerciseArray' which is init like this:
NSString *path = [[NSBundle mainBundle]pathForResource:#"data" ofType:#"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = rootLevel;
[rootLevel release];
When I NSLog it, I get:
exerciseArray: (
{
exerciseName = "Balance Board";
},
{
exerciseName = "Barbell Seated Calf Raise";
},
{
exerciseName = "Calf Press On The Leg Press Machine";
},
{
exerciseName = "Calf Raises- With Bands";
},
So can anyone help me modify the search method to fit the array I have? It seems to have a key in it.
I tried this but it did not work and crashed at line NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
with error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x2d6450
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSLog(#"exerciseArray: %#", exerciseArray);
for (NSString *sTemp in listOfItems)
{
if (sTemp) {
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
}
}
Looks like you have an array of dictionaries with one key/value pair in each dictionary (exerciseName). so for your dataset, it should be this:
for (NSDictionary *dictionary in exerciseArray)
{
NSArray *array = [dictionary objectForKey:#"exerciseName"];
[searchArray addObjectsFromArray:array];
}
...
If you post your VC code related to the array and also your tableview datasource methods, I have can modify it and show you what you need to do.
For searching from table view i have tried this and i got success hope it will help you......
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.searching=NO;
NSString *originalString;
[self.filteredProductArray removeAllObjects];
for (int i=0; i<self.exerciseArray.count; i++)
{
originalString=[[[self.exerciseArray objectAtIndex:i] objectForKey:#"exerciseName"] lowercaseString];
if (!([originalString rangeOfString:[searchText lowercaseString]].location==NSNotFound))
{
[self.filteredProductArray addObject:[self.exerciseArray objectAtIndex:i]];
}
}
if ([searchText isEqual:#""])
{
self.searching=NO;
}
[self.tableView reloadData];
}
I believe NSPredicates are good way to search in arrays.. You can refer to this link.. Using NSPredicate to filter an NSArray based on NSDictionary keys

iPhone/iPad - Problem with Copy values of NSArray into NSMutableArray?

This code is the search code
- (void) searchTableView {
NSLog(#"4");
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in listOfItems)
{
NSArray *array = [dictionary objectForKey:#"Countries"];
[searchArray addObjectsFromArray:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
//NSLog(#"Count - %d",[copyListOfItems count]);
[searchArray release];
searchArray = nil;
}
I want to implement this type of search in my array but i am getting error in below line.
[searchArray addObjectsFromArray:array];
when control comes on this line application shutdown every time can any one help me ?
Thanks
That line will give you an error only if array has nothing in it(nil value).
So check what is the value of array using nslog.

NSMutableDictionary confused over how to use keys with certain code?

I'm getting data from a database and I need to add the string field value and the record id.
However, I need this to work with some existing code...
I'm replacing this (see code below) and getting data from my database.
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.allCategories = dict;
[dict release];
This is the file it loads...
But needs to work with these key and value search functions.
- (void)resetSearch {
NSMutableDictionary *allCategoriesCopy = [self.allCategories mutableDeepCopy];
self.Categories = allCategoriesCopy;
[allCategoriesCopy release];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObject:UITableViewIndexSearch];
[keyArray addObjectsFromArray:[[self.allCategories allKeys]
sortedArrayUsingSelector:#selector(compare:)]];
self.keys = keyArray;
[keyArray release];
}
.
- (void)handleSearchForTerm:(NSString *)searchTerm {
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in self.keys) {
NSMutableArray *array = [Categories valueForKey:key];
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (NSString *name in array) {
if ([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch].location
== NSNotFound)
[toRemove addObject:name];
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
[toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}
Keep getting an error from this code...
NSDictionary *arrayTmp= [[NSDictionary alloc] init];
... loop records
int cid = sqlite3_column_int(statementTMP, 0);
NSString *category = [[NSString alloc]
initWithUTF8String:(char *)sqlite3_column_text(statementTMP, 1)];
[arrayTmp setObject:category forKey:[NSString stringWithFormat:#"%i", cid]];
Error caused by line above
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x4d4c500' * Call stack at first throw *
... end loop
self.allCategories = arrayTmp;
[arrayTmp release];
The error message says you are sending 'count' to an instance of NSString.
My guess is that the plist at 'path' (the plist file) has a string as one of the values. As you iterate through 'Categories' you expect each value to be an array (i.e. 'NSMutableArray *array = [Categories valueForKey:key];') and send that value a 'count' message.
One of the objects you expect to be an array is a string.
setValue is key-value coding method. You should use setObject:forKey: instead.
arrayTmp should be inited with [[NSMutableDictionary alloc] init].
First of all, you're using wrong method:
[arrayTmp setValue:category forKey:[NSString stringWithFormat:#"%i", cid]];
which method used for key-value coding (you can find more about KVO here).
The method you're looking for is:
[arrayTmp setObject:category forKey:[NSString stringWithFormat:#"%i", cid]];
Moreover, you should NSMutableDictionary. The NSDictionary you're using does not support mutation. (you can find more info on NSMutableDictionary here).
Cheers,
Pawel