Adding Object NSMutableArray with ForEach Loop - iphone

here is what i am trying to do:
NSMutableArray *objectNames = [[NSMutableArray alloc] init];
for (Object *o in objectList){
if (![objectNames containsObject:o.name]) {
[objectNames addObject:o.name];
}
}
I am trying to go through an array of objects, then take the objects name (a string) and add it to a string array of objectNames.
This code works in the simulator just fine. but when i run it on the device i get this error.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'

One or more of the objects in objectList has it's name property set to nil. This leads to you trying to insert just nil into objectNames, which gives you the exception.
If it's OK for an object to have a name of nil, what you need to do is to check for this before you insert into objectNames:
NSMutableArray *objectNames = [[NSMutableArray alloc] init];
for (Object *o in objectList){
if (name && ![objectNames containsObject:o.name]) {
[objectNames addObject:o.name];
}
}

Looks like one of your Objects doesn't have an name set correctly

Related

NSInvalidArgumentException while adding NSDictionary to NSArray

I have declared NSDictionary *dict; globally and trying to add this dictionary into array but getting an Error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
My Code :
dict = #{#"Testcase" : Testcase, #"TestResult" : TestResult,#"Message": Message};
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
How to solve this Error ?
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[__NSArrayM
insertObject:atIndex:]: object cannot be nil'
Problem : From the above ErrorMessage, it is clear that you are trying to add NULL value in your NSDictionary.
Explanation : At the time of inserting Value to your NSDictionary , note that the Value shouldn't be NULL and Non-String. You are trying to add Value of Variable to your Dictionary directly. Instead you should try to add like this :
Answer :
[NSString stringWithFormat:#"%#",Testcase];
Benefits :
It will add String-Value.
It will add Empty-Value when your Value of Variable is NULL.
dict = [NSDictionary dictionaryWithObjectsAndKeys:Testcase:#"Testcase", TestResult: #"TestResult",Message, #"Message", nil];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
Make sure TestcaseId is not more than your array count
[myArray addobject:dict];
or
[myArray replaceObjectAtIndex:TestcaseId withObject:dict]

NSInvalidArgumentException', reason: '-[__NSArrayM name]: unrecognized selector sent to instance 0x75786f0'

CellData *cellData = [self.tableElements objectAtIndex:indexPath.row];
NSLog(#"name is %#",[self.tableElements objectAtIndex:1]);
cell.lbl.text = cellData.name;
here am getting the error when i able to access my NSObject class variables.
self.tableElements is an array with elements parsed.
cellData is not what you think it is, you think it is a CellData object but it is actually an NSArray, double check what self.tableElements is holding.

Initializing NSMutableDictionary to create a filter

This is a continuation question I had from a post yesterday: Initializing NSMutableDictionary
(It's probably a stupid question, but I don't see what I'm doing wrong.)
I liked the idea of just having "All" listed in my TableView that pops up when the filter button is pressed in order to indicate no filter applied. I get my data from my model class sorted correctly into an NSMutableArray like so:
NSArray *anArray = [dmgr.CategoryDictionary allKeys];
self.CategoriesArray = [anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
[self.CategoriesArray insertObject:#"All" atIndex:0]; // line causes crash
But I want to add the "All" option to the CategoriesArray to show in the UITableView. When I try to run it, it crashes at that line because of
NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x59baba0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x59baba0'
Any help would be appreciated. Thanks!
sortedArrayUsingSelector: returns an immutable array which is why you can't add to it. Do this to get a mutable copy.
self.CategoriesArray = [NSMutableArray arrayWithArray:[anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]];
It seems CategoriesArray is actually a NSArray instance. From Apple's doc :
-(NSArray *)sortedArrayUsingSelector:(SEL)comparator;

Editting NSMutableDictionary in array

I've got a question: I've got an NSMutableArray, which I pass on in multiple views. In one of the views, a value of an object inside that array (NSMutableDictionary) can be editted. I do that using the code:
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:selectedItem copyItems:YES];
NSMutableString *newLoc = [[NSMutableString alloc] initWithString:locationField.text];
[tester setValue:newLoc forKey:#"Location"];
[selectedList replaceObjectAtIndex:[selectedList indexOfObject:selectedItem] withObject:tester];
The problem I'm having with this, is replacing that object in selectedList. It gives the error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0xa631e30'
It works if I copy the new item in a copy-array of selectedList, and alloc selectedList with the new value again, but the other views are having problem finding that same list again in the array (new allocated location).
TL;DR version: how can I edit a value (through replacing?) inside an NSMutableArray? Why doesn't replaceObjectAtIndex work?
EDIT: It was immutable indeed. Still, the main question remains:
I've got:
NSMutableDictionary *tester = [[NSMutableDictionary alloc] initWithDictionary:geselecteerdItem copyItems:YES];
[geselecteerdItem setValue:nieuweLoc forKey:#"Location"];
[geselecteerdeLijst replaceObjectAtIndex:[geselecteerdeLijst indexOfObject:tester] withObject:geselecteerdItem];
When I'm using: [warningList replaceObjectAtIndex:[warningList indexOfObject:geselecteerdeLijst] withObject:keuzeLijst], it gives me an outofbounds error because the index of the array geselecteerdeLijst obviously changed inside warningList. Any idea's?
selectedList is an immutable array, which doesn't support any modifications. You can do something like this, though:
NSMutableArray *tmp = [NSMutableArray arrayWithArray: selectedList];
[tmp replaceObjectAtIndex:[selectedList indexOfObject:selectedItem] withObject:tester];
EDIT: To clarify, __NSArrayI is a concrete immutable subclass of NSArray, __NSArrayM is a mutable one. You should not rely on the private class names, but since they speak for themselves, you can at least know which is mutable and which is not.

NSCFArray length]: error, array regex

StringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
//Regex Out Artist Name
//NSString *regEx = ;
NSArray *iTunesAristName = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([iTunesAristName isEqual:#""]) {
NSLog(#"Something has messed up");
//Regex Out Song Name
}else{
NSLog(iTunesAristName);
}
NSLog(iTunesAristName);
[stringReply release];
I just keep getting this error ?
2010-09-29 21:15:16.406 [2073:207] *** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800
2010-09-29 21:15:16.406 [2073:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800'
2010-09-29 21:15:16.407 [2073:207] Stack: (
please help its driving me crazy
The first argument to NSLog is supposed to be a format string. You're passing an NSArray. When the function tries to treat your array as a string, you get that error. Instead, use NSLog(#"%#", iTunesAristName);.
Chuck has answered your question, but I've noticed something else that is problematic.
NSArray is an array, not a string, so [iTunesArtistName isEqual:#""] will never return true, because they are different classes. Even if iTunesArtistName was a string, it should be compared using the isEqualToString: method, not isEqual:.
If you want to extract only the artist's name, you might be able to do this:
NSArray *matches = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([matches count] == 0)
{
NSLog(#"Could not extract the artist name");
}
else
{
NSString *iTunesArtistName = [matches objectAtIndex:0];
NSLog(#"Artist name: %#", iTunesArtistName);
}
I see you're using RegexKitLite, make sure you import libicucore.dylib, i was getting the same error until i imported that library.