I have a json parser with AFHTTPRequestOperation.
my parsing code:
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
and I have a NSMutableArray
#property (strong, nonatomic) NSMutableArray * myDirectory;
First, I load main 10 items and add with this:
self.myDirectory = responseObject;
After, when I want to load more 10 items, I tried many things but error is:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '-[__NSCFArray insertObject:atIndex:]:
mutating method sent to immutable object'
I tried:
[self.myDirectory insertObject:[responseObject mutableCopy] atIndex:[self.myDirectory count]];
[self.myDirectory addObjects:responseObject];
etc...
What I must use?
AFNetworking returns immutable arrays (NSArray), so you must assign it like:
self.myDirectory = [responseObject mutableCopy]
did you try [myArray addObjectsFromArray:otherArray];
AFNetworking returns immutable arrays. So try this:
self.myDirectory = [responseObject mutableCopy];
When you want to add the second responseObject to previous self.myDirectory try this:
[self.myDirectory addObjectsFromArray: responseObject];
Check this answer for a clear concept about copy and mutableCopy.
Hope this helps.. :)
EDIT:
NSArray *arr = [[NSArray alloc] initWithObjects:#"A",#"B", nil];
NSArray *arr1 = [[NSArray alloc] initWithObjects:#"C",#"D", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:#"E",#"F", nil];
NSMutableArray *mutableArray= [[NSMutableArray alloc] init];
mutableArray = [arr mutableCopy];
[mutableArray addObjectsFromArray:arr1];
[mutableArray addObjectsFromArray:arr2];
NSLog(#"%#",mutableArray);
Related
why adding string to nsmuarray not work?
firstly, i add the a NSDictionary by keypath to the NSMutableArray,
its work.
after that i want to add one more string to that but its not work.
NSMutableArray *_joinornot;
_joinornot = [[NSMutableArray alloc] init];
NSDictionary *tempobject = [[NSDictionary alloc] init];
_joinornot = [tempobject valueForKeyPath:#"groupid"];
until now everything work.
[_joinornot addObject:#"111"];<----unrecongnized selector sent to instance
if _joinornot = [tempobject valueForKeyPath:#"groupid"]; returns nil, then your array will be nil, and then you cant call addObject. so maybe add a nil check
Looks like "_joinornot" it's not an NSMutableArray or NSMutable data type, try to see what kind of object it is:
NSLog(#"%#", [_joinornot class]);
If it is not a subclass of Mutable type you can't add objects to him.
Try below code:
Before adding object just check for nil.
NSMutableArray *_joinornot;
_joinornot = [[NSMutableArray alloc] init];
NSDictionary *tempobject = [[NSDictionary alloc] init];
_joinornot = [tempobject valueForKeyPath:#"groupid"];
if (_joinornot==nil) {
_joinornot = [[NSMutableArray alloc] init];
[_joinornot addObject:#"111"];
}
else{
[_joinornot addObject:#"111"];
}
Edit:
May be it's converted to NSArray so it will be no more mutable, try with
_joinornot = [[tempobject valueForKeyPath:#"groupid"] mutableCopy];
Can someone help me out on this:
Im creating a property in my TableVC.m file :
#property NSMutableArray *savingBeaconSpecs;
In my Viewdidload I instantiate the array:
NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];
Now I do requests to the server, and I want to save the returned JSON into objects and save these each time in the array. So I did the following in the ConnectionDidFinishLaunching:
self.artworkArray = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:&err];
NSLog(#"Log ArtworkArray in ConnectionDidFinishLoading%#", self.artworkArray);
And:
Artwork *artwork = [[Artwork alloc]init];
artwork.title = [self.artworkArray valueForKey:#"name"];
artwork.artist = [[self.artworkArray objectForKey:#"artist"] valueForKey:#"name"];
artwork.CreationYear = [self.artworkArray valueForKey:#"creationYear"];
artwork.categorie = [[self.artworkArray objectForKey:#"exposition"] valueForKey:#"name"];
Now I want to save this object into the savingBeaconSpecs NSMutableArray
[self.savingBeaconSpecs addObject:artwork];
But the NSMUtableArray savingBeaconSpecs always returns 0 when i try log his content
Anyone please?
Because you declare it locally in your viewDidLoad :
NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];
you should use
self.savingBeaconSpecs = [[NSMutableArray alloc]init];
and
[self.savingBeaconSpecs addObject:artwork];
and declare your property as (without the first capital S)
#property NSMutableArray *savingBeaconSpecs;
To instantiate the array, you should do:
self.savingBeaconSpecs = [[NSMutableArray alloc] init];
or equally good:
self.savingBeaconSpecs = [NSMutableArray array];
I've gone crazy trying to find a leak with an NSMutableArray:
NSMutableArray *mutablearray =[[[[NSMutableArray alloc] initWithArray: array] mutableCopy] autorelease];
Finally I understood that I need to autorelease twice my mutablearray because initWithArray is +1 and mutableCopy is +1 too.
Then I'm doing:
NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array] autorelease];
mutablearray = [[mutablearray mutableCopy] autorelease];
But, it's correct to do?:
NSMutableArray *mutablearray = [[[[[NSMutableArray alloc] initWithArray: array] autorelease] mutableCopy] autorelease];
Thanks
The first question comes in mind is WHY you are initializing and also making a mutable copy at the same time initWithArray gives a new instance with new memory and you can use it.Then why creating a mutable copy of it?
Use
NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array]autorelease];
This gives you a mutable instance .So no need for calling mutablecopy anyway
OR
NSMutableArray *mutablearray = [[array mutableCopy]autorelease];
I keep getting this error when i try to bind a nsobject to a segment control
UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60
2013-01-22 12:44:58.115 Momentum[39936:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60'
I have verified that my core data object has data.
NSarray *arrayuserlocation = [[MMIStore defaultStore] loadAllUserLocation];
UISegmentedControl *segControl = [[UISegmentedControl alloc]initWithItems:arrayuserlocation];
[segControl addTarget:self action:#selector(didChangeSegmentControl:) forControlEvents:UIControlEventValueChanged];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setTintColor:[UIColor grayColor]];
EDIT
To the answer the question below
- (NSMutableArray *)loadAllUserLocation
{
if (!allItems) {NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:#"UserLocation"];
[request setEntity:e]
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:#"Fetch failed"
format:#"Reason: %#", [error localizedDescription]];
}
allItems = [[NSMutableArray alloc] initWithArray:result];
}
return allItems;
It returns an array
I was able to solve my problem by doing the following.
NSArray *arraylocation = [[MMIStore defaultStore] loadAllUserLocation];
NSMutableArray *newarray = [[NSMutableArray alloc] init];
for (UserLocation *user in arraylocation)
{
NSLog(#"%# found", user.locationNm);
[newarray addObject:user.locationNm];
}
And using newarray as the datasource for the segment control.
As I mentioned in comments, the issue is that you are passing userlocation objects instead of NSString or UIImage objects required.
As per the documentation your items array should be "an array of NSString objects (for segment titles) or UIImage objects (for segment images)."
You need to fetch the strings from user location as,
NSarray *arrayuserlocation = [[[MMIStore defaultStore] loadAllUserLocation] valueForKey:#"locationNm"];//use the param name here
This should give you an array of all strings from the array of objects.
The problem is, the arrayuserlocation array should contain NSStrings instead of NSManagedObjects.
The code that's throwing the exception is expecting you to pass it an NSString (this is the object that responds to isEqualToString:). However, you are passing it a UserLocation object. You need to load up arrayuserlocation with strings from the UserLocation object, not just send an array of objects themselves.
I have a global NSMutableArray and I need to update it with values. NSMutableArray is defined in the .h as follows;
#property (strong, nonatomic) NSMutableArray *myDetails;
In the viewDidLoad pre-populate like this;
NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:#"1", #"rowNumber", #"125", #"yards", nil];
NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"2", #"rowNumber", #"325", #"yards", nil];
NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys:#"3", #"rowNumber", #"525", #"yards", nil];
self.myDetails = [[NSMutableArray alloc] initWithObjects:row1, row2, row3, nil];
Then when the user changes a text field this code is run this;
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSObject *rowData = [self.myDetails objectAtIndex:selectedRow];
NSString *yards = textField.text;
[rowData setValue:yards forKey:#"yards"];
[self.myDetails replaceObjectAtIndex:selectedRow withObject:rowData];
}
When stepping through the code on the line [rowData setValue:yards forKey:#"yards"]; it returns this error;
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
The array is mutable, but what is in it... NSDictionary... is not. You grab an object out of the array...
NSObject *rowData = [self.myDetails objectAtIndex:selectedRow];
and then you try to mutate that object...
[rowData setValue:yards forKey:#"yards"];
The object in the array is the thing you are changing... and it is NSDictionary, immutable, and you can not change it. If you want the dictionary to be mutable, you have to use NSMutableDictionary