Why am I getting NSFastEnumerationMutationHandler crash all of a sudden in my code. I am blank why this crash popped up all of a sudden and how to squash it.
Crash Error:
**** Terminating app due to uncaught exception 'NSGenericException', reason: '* **Collection <__NSArrayM: 0x610000859410> was mutated while being enumerated.'*
You must be trying to change an array while you using fast enumeration.
Example
for ( id anObject in anArray ) {
if ( /* anObject satisfies some condition */ ) {
[anArray removeObject:anObject];
}
}
That shouldn't be done. Use a different array or probably filteredArrayUsingPredicate: method to filter. Remedy, however, depends on what you're trying to do.
Came here looking for a solution and ended up taking a copy of the original array to get around the issue.
for (NSObject *object in [array copy]) {
if(condition) {
[array removeObject....]
break;
}
}
Related
I have 2 NSMutableArrays and i want to put certain object form 1 array to the other array I have already code written but it doesnt work and it gives me this error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 296 beyond bounds [0 .. 295]'
* First throw call stack:
(0x1c9a012 0x10d7e7e 0x1c3c0b4 0x2f04 0xb20b90 0x1c59376 0x1c58e06 0x1c40a82 0x1c3ff44 0x1c3fe1b 0x1bf47e3 0x1bf4668 0x1f65c 0x252d 0x2455)
libc++abi.dylib: terminate called throwing an exception
Initialisierung in viewdidload:
arrayLine1 =[[NSMutableArray alloc] initWithCapacity:80000];
arrayLine1a =[[NSMutableArray alloc] initWithCapacity:70000];
line1tagzahl=0;
line1tagzahl2=0;
passing code:
for (int a=0; a<10; ) {
[arrayLine1a insertObject:[arrayLine1 objectAtIndex:line1tagzahl2] atIndex:line1tagzahl2];
line1tagzahl2=line1tagzahl2+1;
a=a+1;
}
function to create objects in array(this function is called very fast and frequently) :
for (float a=0; a<0.8; ) {
UIImageView *line1 =[[UIImageView alloc] initWithFrame:CGRectMake(Startpoint1.center.x-(w/2),Startpoint1.center.y-kurve1yf+a,w,h)];
line1.image=[UIImage imageNamed:#"Unbenannt"];
line1.tag=line1tagzahl;
[self.view addSubview:line1];
[arrayLine1 insertObject:line1 atIndex:line1tagzahl];
line1tagzahl=line1tagzahl+1;
a=a+0.1;
}
now you should have more information
and if you ask i have more than 10 objects in array2
Seems you haven't read the error message you posted. You have 296 objects in the array. But if you just want to copy the array, why don't you... er... copy it?
NSMutableArray *secondArray = [firstArray mutableCopy];
Is there any problem with using a default method -addObjectsFromArray: of NSMutableArray class?
- (void)serverGotResponse:(NSArray *)objects {
[_myMutableArray addObjectsFromArray:objects];
}
To prevent out of bounds exception, you should use count property:
while (i < array.count) {
// do something ..
i++;
}
if you are copying all the objects, you can just copy the entire array:
array2 = [array1 copy]
You are trying to add the first 10 objects, but your exception was thrown on index 296, which is the first index that comes out of bounds.
For some reason, your while condition is not working properly, so I suggest you start looking there
I also suggest you use a for, it's actually more simple and straightforward than a potential infinite loop
for (int a = 0; a < 10; a++) {
[array1 insertObject:[array2 objectAtIndex:a] atIndex:a];
}
This is the error I am reciving when I select a value from my UITableView
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_ILLEGAL_ARGUMENT_ERROR (string Motron, pattern
libc++abi.dylib: terminate called throwing an exception
(lldb)
This is the first time I have had an error like this.. I am receiving it in my tableview method didSelectRowAtIndexPath on the second line of code shown below
// This predicate restricts the filterDataArray to the related values of the selected index
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K like %#",#"MASTER",cell.textLabel.text];
filterDataArray = [dataArrayOfDictionaries filteredArrayUsingPredicate:predicate];
I have logged out the array of dictionaries and this is what one of the dictionary values looks like.
{
HASM = 1;
ISM = 0;
ISV = 0;
MASTER = Merc;
MANURE = 96;
}
There are about 60 or 17 of these dictionaries in the array.
any help would be greatly appreciated.
I think you are not correctly using predicate in this case, try this
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"MASTER CONTAINS [c]%#", cell.textLabel.text];
filterDataArray = [dataArrayOfDictionaries filteredArrayUsingPredicate:predicate];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I check if a UILabel's value is more than 0 in an if statement?
Why is the following code not working?
if([Period2 isEqualToString:#"PSHEEC"])
{
NSLog(#"TEST");
}
I am getting this error:
2011-12-02 08:45:52.579 iDHSB[7605:707] -[UILabel isEqualToString:]:
unrecognized selector sent to instance 0x4884c50 2011-12-02
08:45:52.581 iDHSB[7605:707] * Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[UILabel
isEqualToString:]: unrecognized selector sent to instance 0x4884c50'
* First throw call stack: (0x323e28bf 0x35cfa1e5 0x323e5acb 0x323e4945 0x3233f680 0x3152b191 0x9c905 0x316cf871 0x323e5814
0x323407e1 0x323403ff 0x34767e5b 0x323e4ab3 0x3233f680 0x323e5814
0x323407e1 0x33dcb43d 0x33dde8dd 0x323b6b03 0x323b62cf 0x323b5075
0x323384d5 0x3233839d 0x378b7439 0x315558e1 0x2d77 0x27c0) terminate
called throwing an exception[Switching to process 7171 thread 0x1c03]
(gdb)
The error you are getting is propably due to memory mamagement issues, the pointer of Period2 is not pointing to you string any more and is now pointing to some label. Make sure you have retained it correctly.
On an other note, variable, properties, methods should not start with a capital, and if you label is a properties you should use self.period2 .
if period2 is your label use...
[period2.text isEqualToString:#"PSHEEC"]
It may be helpful
NSString *str = label.text;
if([str isEqualToString:#"PSHEEC"])
{
NSLog(#"Equal");
}
else
{
NSLog(#"Not Equal");
}
What is Period2? You can't test equivalence that way. If you want to test string equivalence, you need to do something like the following:
(assuming Period2 is an NSString)
if ([Period2 isEqualToString:#"PSHEEC]) {
NSLog(#"They are equal.");
}
I am getting a strange error message from the core data when trying to save
but the problem that the error is not reproducible ( it appears at different times when doing different tasks)
the error message:
Unresolved error Domain=NSCocoaErrorDomain Code=1560 UserInfo=0x14f5480 "Operation could not be completed. (Cocoa error 1560.)", {
NSDetailedErrors = (
Error Domain=NSCocoaErrorDomain Code=1570 UserInfo=0x5406d70 "Operation could not be completed. (Cocoa error 1570.)",
Error Domain=NSCocoaErrorDomain Code=1570 UserInfo=0x14f9be0 "Operation could not be completed. (Cocoa error 1570.)"
);
}
and the method that generates the error is:
- (IBAction)saveAction:(id)sender {
NSError *error;
if (![[self managedObjectContext] save:&error]) {
// Handle error
NSLog(#"Unresolved error %#, %#, %#", error, [error userInfo],[error localizedDescription]);
exit(-1); // Fail
}
}
any idea for the reason of this message ? giving that it appears at random times
It means there's a mandatory property has been assigned nil. Either in your *.xcodatamodel check the "optional" box or when you are saving to the managedObjectContext make sure that your properties are filled in.
If you're getting further errors after changing your code to suit the two requirements try cleaning your build and delete the application from your iPhone Simulator/iPhone device. Your model change may conflict with the old model implementation.
Edit:
I almost forgot here's all the error codes that Core Data spits out:
Core Data Constants Reference
I had trouble with this before and I realised I unchecked the correct optional box. Such trouble finding out the problem. Good luck.
I struggled with this for a little while myself. The real problem here is that the debugging you've got isn't showing you what the problem is. The reason for this is because CoreData will put an array of NSError objects in the "top level" NSError object it returns if there is more than one problem (This is why you see error 1560, which indicates multiple problems, and an array of error 1570s). It appears that CoreData has a handful of keys it uses to stash information in the error it returns if there is an issue that will give you more useful information (Such as the entity the error occurred on, the relationship/attribute that was missing, etc). The keys you use to inspect the userInfo dictionary can be found in the reference docs here.
This is the block of code I use to get reasonable output from the error returned during a save:
NSError* error;
if(![[survey managedObjectContext] save:&error]) {
NSLog(#"Failed to save to data store: %#", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(#" DetailedError: %#", [detailedError userInfo]);
}
}
else {
NSLog(#" %#", [error userInfo]);
}
}
It will produce output that tells you the fields that are in missing, which makes fixing the problem significantly easier to deal with.
I'm throwing this in as an answer, even though it's really more of an embellishment to Charles' snippet. The straight output from NSLog can be a mess to read and interpret, so I like to throw in some white space and call out the value of some critical 'userInfo' keys.
Here's a version of the method I've been using. ('_sharedManagedObjectContext' is a #define for '[[[UIApplication sharedApplication] delegate] managedObjectContext]'.)
- (BOOL)saveData {
NSError *error;
if (![_sharedManagedObjectContext save:&error]) {
// If Cocoa generated the error...
if ([[error domain] isEqualToString:#"NSCocoaErrorDomain"]) {
// ...check whether there's an NSDetailedErrors array
NSDictionary *userInfo = [error userInfo];
if ([userInfo valueForKey:#"NSDetailedErrors"] != nil) {
// ...and loop through the array, if so.
NSArray *errors = [userInfo valueForKey:#"NSDetailedErrors"];
for (NSError *anError in errors) {
NSDictionary *subUserInfo = [anError userInfo];
subUserInfo = [anError userInfo];
// Granted, this indents the NSValidation keys rather a lot
// ...but it's a small loss to keep the code more readable.
NSLog(#"Core Data Save Error\n\n \
NSValidationErrorKey\n%#\n\n \
NSValidationErrorPredicate\n%#\n\n \
NSValidationErrorObject\n%#\n\n \
NSLocalizedDescription\n%#",
[subUserInfo valueForKey:#"NSValidationErrorKey"],
[subUserInfo valueForKey:#"NSValidationErrorPredicate"],
[subUserInfo valueForKey:#"NSValidationErrorObject"],
[subUserInfo valueForKey:#"NSLocalizedDescription"]);
}
}
// If there was no NSDetailedErrors array, print values directly
// from the top-level userInfo object. (Hint: all of these keys
// will have null values when you've got multiple errors sitting
// behind the NSDetailedErrors key.
else {
NSLog(#"Core Data Save Error\n\n \
NSValidationErrorKey\n%#\n\n \
NSValidationErrorPredicate\n%#\n\n \
NSValidationErrorObject\n%#\n\n \
NSLocalizedDescription\n%#",
[userInfo valueForKey:#"NSValidationErrorKey"],
[userInfo valueForKey:#"NSValidationErrorPredicate"],
[userInfo valueForKey:#"NSValidationErrorObject"],
[userInfo valueForKey:#"NSLocalizedDescription"]);
}
}
// Handle mine--or 3rd party-generated--errors
else {
NSLog(#"Custom Error: %#", [error localizedDescription]);
}
return NO;
}
return YES;
}
This allows me to see the value for 'NSValidationErrorKey', which, when I encountered the issue from the OP, pointed directly to the non-optional Core Data entities that I'd forgot to set before trying to save.
The problem touched me, when I save second record to CoreData.
All not optional fields (relationship) was filled without nil as well, but in the error output I'd notice, that one of fields in first saved object had became nil. Strange a little? But the reason is quite trivial - one to one relationship which nullify first object, when I set it in the second.
So, the scheme is:
"Parent" with relationship "child" One to One
Create Child 1, set parent. Save - OK
Create Child 2, set parent. Save - Error, Child 1.Parent == nil
(behind the scene child 2 did nullify child 1 parent)
Changing the relationship in Parent from One to One to Many to One solved this task.
I had transient property of type int that wasn't optional. Obviously, when it was set to 0, 1570 error appear. Just changed all my transient properties to optional. Nil-check logic can be implemented in code if necessary.
I means that your model failed to validate, which could happen for a number of reasons: unused property in your model, missing value that's marked as required.
To get a better understanding of what exactly went wrong, put a breakpoint in a place where you are ready to save your object, and call one of the validateFor... method variants, like:
po [myObject validateForInsert]
More detailed information about the problem is in error description. Successful validation means you will get no output.
It helped me. Check this one too.
Check the optional box in your *.xcodatamodel objects
Okay, I have an Book Object Class. That has Book.name and Book.id as extensions.
Book.name being NSString. Which is what should be searchable and listed in UITableView.
NSMutableArray *matchingSymptomsArray = [[NSMutableArray alloc] initWithCapacity:50];
for(NSMutableArray *letterArray in DataArray){
for(Book *bk in letterArray){ //Heres the ERROR
NSLog(#"Uptil NEW");
if([bk matchesSearchString:searchString]){
//I couldnt reach here, according to debugging and Logs//
[matchingSymptomsArray addObject:bk];
}
}
}
DataArray is where all the Books are held.. more than a hundred. Its the main datasource for the UITableView. I'm trying to search and match each letter of the string with the Book.name, but even before I can begin, I cant do the
for(Book *bk in LetterArray) //producing the error..
What seems to be the problem?
This is the error in console
**** -[Book countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x55c110
2009-09-29 06:17:41.073 Smart Diagnosis[3897:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[Book countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x55c110'
Any work arounds??
thanks for your help :)
Try this:
for (id bk in letterArray) { }
Did you check that letterArray isnt nil or empty?
Did you check that letterArray isnt nil or empty?