NSGenericException when trying to remove an object from an array in iOS - ios

I have checked the examples of how to fix this problem but I am still facing it..
My Code is
NSEnumerator *enu= [obstacles objectEnumerator];
NSMutableArray *delete = [[NSMutableArray alloc] init];
Object *obj;
while ((obj=[enu nextObject])!=nil)
{
if ([obj isKindOfClass: [BObject class]] && CGPointEqualToPoint(obj.position, point) ) {
[view.objects removeObject: obj];
//[obstacles removeObject: obj];
[delete addObject:obj];
}
}
[obstacles removeObjectsInArray:delete];
[delete release];
the error is *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x6a97ba0> was mutated while being enumerated.'
Update: My code work if there is only one object in my obstacles array. it fails with multiple objects..

Related

NSMutableDictionary issues

I'm trying to insert an object in my dictionary, but first, I want to test if it's a NSMutableDictionary :
if ([[[self.response objectForKey:#"X"] objectAtIndex:0] isKindOfClass:[NSMutableDictionary class]]) {
[[[self.response objectForKey:#"X"] objectAtIndex:0]setObject:#"Hello" forKey:#"Y"];
}
I get this issue :
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
You can check for the same selector which is crashing,
if([object respondsToSelector:#selector(setObject:forKey:)]) {
//If YES, it is NSMutableDictionary
}
else {
//Not mutable
}
Hope that helps!
Use isMemberOfClass instead of isKindOfClass
Try this,
if ([[[self.response objectForKey:#"X"] objectAtIndex:0] isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[self.response objectForKey:#"X"] objectAtIndex:0]];
[dict setObject:#"Hello" forKey:#"Y"];
NSMutableArray *xArray = [NSMutableArray arrayWithArray:[self.response objectForKey:#"X"]];
[xArray replaceObjectAtIndex:0 withObject:dict];
[self.response setObject:xArray forKey:#"X"];
}

iOS: Error When Scrolling Quickly Through Photos?

I'm currently using MWPhotoBrowser in my app and when I scroll quickly through images I get the following error:
Received memory warning.
2014-02-17 16:42:35.117 App[10803:60b] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x156f3160> was mutated while being enumerated.'
*** First throw call stack:
(0x2e71de83 0x38a7a6c7 0x2e71d971 0x151167 0x15139b 0x311643ff 0x3116446f 0x31164665 0x31164805 0x3111ea67 0x38f5f0af 0x38f6072f 0x38f61959 0x2e6e85b1 0x2e6e6e7d 0x2e651471 0x2e651253 0x3338b2eb 0x30f06845 0xff035 0x38f73ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
I'm currently loading images stored in the app locally.
This is the method throwing the exception:
- (void)releaseAllUnderlyingPhotos:(BOOL)preserveCurrent {
for (id p in _photos) {
if (p != [NSNull null]) {
if (preserveCurrent && p == [self photoAtIndex:self.currentIndex]) {
continue; // skip current
}
[p unloadUnderlyingImage];
}
} // Release photos
}
Any help would be greatly appreciated!
I don't know exactly which version of MWPhotoBrowser you are using, but in the latest one here, the releaseAllUnderlyingPhotos: method reads:
- (void)releaseAllUnderlyingPhotos:(BOOL)preserveCurrent {
// Create a copy in case this array is modified while we are looping through
// Release photos
NSArray *copy = [_photos copy];
for (id p in copy) {
if (p != [NSNull null]) {
if (preserveCurrent && p == [self photoAtIndex:self.currentIndex]) {
continue; // skip current
}
[p unloadUnderlyingImage];
}
}
// Release thumbs
copy = [_thumbPhotos copy];
for (id p in copy) {
if (p != [NSNull null]) {
[p unloadUnderlyingImage];
}
}
}
Please note these two lines:
NSArray *copy = [_photos copy];
for (id p in copy) {
Before iterating through _photos, a new array copy is created to protect this iteration from _photos being modified in other places.
In your code, releaseAllUnderlyingPhotos: is deleting objects straight from the _photos array, but this array could be modified in other parts of the code, for example, didReceiveMemoryWarning, as it is your case.
Once you modify your code to iterate over a copy of _photos in releaseAllUnderlyingPhotos: your problem should go away.

IOS [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

I am trying to create a dynamic slide menu like facebook.
I have to get some data from a json request. and display this data in one of the sections of the slide table view.
I'm new in objective-c, I also tried with NSMutableArrays and I had an error.
In other table views I do the same but without sections. only one MutableArray and I can show the table.
I do something like this:
-(void) requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject{
NSArray *projectsObjectArray = [parsedObject objectForKey:#"projects"];
[self createMainNavigationController];
self.section1 = [NSArray arrayWithObjects:#"Profile", #"Notifications", #"Exit", nil];
self.section2 = [NSArray arrayWithObjects:#"Main", nil];
for (NSDictionary *projectObject in projectsObjectArray)
{
Project *newProject = [[Project alloc] init];
newProject.title= [projectObject objectForKey:#"title"];
self.section3 = [NSArray arrayWithObject:newProject.title];
}
self.menu = [NSArray arrayWithObjects:self.section1, self.section2, self.section3, nil];
[menuTableView reloadData];
}
I am having this error :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
It is because
newProject.title= [projectObject objectForKey:#"title"];
is nil, and you are trying to add it to the array. Check to see what that value is by logging it
NSLog(#"%#", [projectObject objectForKey:#"title"]);

iOS - Terminating app due to uncaught exception 'NSInvalidArgumentException'

So I am really new at developing for iOS, and I've been doing my best, to search for an answer, to debug it and anything I could come up with.
Though, I haven't been able to find a solution to the issue.
I've been trying to fetch an external JSON document, which works fine, but when it comes to parsing it, it buggers up.
First of all, this is the error message I'm getting, the whole lot of it.
2013-01-31 22:40:19.261 demodh[6205:c07] View Loaded
2013-01-31 22:40:19.479 demodh[6205:c07] -[__NSCFStringcountByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90
2013-01-31 22:40:19.480 demodh[6205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x28d3 0xbd6589 0xbd4652 0xbd589a 0xbd460d 0xbd4785 0xb21a68 0x4615911 0x4614bb3 0x4652cda 0x1c358fd 0x465335c 0x46532d5 0x453d250 0x1c16f3f 0x1c1696f 0x1c39734 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x1d6d 0x1c95 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
And this is the code I'm using at the moment:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for(NSDictionary *dict in allDataDictionary)
{
if (![allDataDictionary isKindOfClass:[NSDictionary class]])
{
NSLog(#"2Unable to process temp array because it's an instance of %#", [allDataDictionary class]);
}
else
{
for(NSDictionary *deal in dict)
{
if (![deal isKindOfClass:[NSDictionary class]])
{
NSLog(#"Unable to process temp array because it's an instance of %#", [deal class]);
}
else
{
NSString *title = [deal objectForKey:#"title"];
NSLog(title);
}
}
}
}
}
And the JSON I'm loading is: Link
I hope you're able to assist me in finding a solution.
The problem is you're fast-enumerating a NSDictionary and expecting to get its values, when in fact you'll get its keys. When you try to fast-enumerate an NSString you get the assertion you're seeing. You probably wanted this:
for(NSObject *key in allDataDictionary) {
NSDictionary *dict = allDataDictionary[key];
...
for (NSObject *dealKey in dict) {
NSDictionary *deal = dict[dealKey];
}
...
Alternatively, if you really want to enumerate the values and don't need the keys:
for(NSDictionary *dict in [allDataDictionary allValues]) {
...

Removing an object from a mutable dictionary throws an exception

I'm getting an exception after attempting to remove an object from a NSMutableDictionary. The relevant code follows. The 'settings' is passed to the method and can be a NSDictionary or a NSMutableDictionary.
NSMutableDictionary *mutableSettings = nil;
if ([settings isKindOfClass:[NSMutableDictionary class]])
mutableSettings = (NSMutableDictionary *)settings;
else
mutableSettings = [[[NSMutableDictionary alloc] initWithDictionary:settings] autorelease];
[mutableSettings removeObjectForKey:#"akey"];
This crashes with
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object'
Whats wrong with this? Thanks.
The problem here is that both NSDictionary and NSMutableDictionary return __NSCFDictionary as their class, due to the fact that NSDictionary is a class cluster.
I think you will just have to make a mutable copy of the settings dictionary whether it is mutable or not.
NSMutableDictionary *mutableSettings = [settings mutableCopy];

Resources