I am trying take the values of an array of CLLocation's made from a JSON request and assign them to a property that is a NSMutable array.
Here the relevant controller code:
- (void)viewDidLoad
{
[super viewDidLoad];
//using a background thread to receive the json call so that the UI doesn't stall
dispatch_async(directionQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:openMapURL];
[self performSelectorOnMainThread:#selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
NSLog(#"%#", self.coordinates);
}
- (void)fetchedData:(NSData *)responseData
{
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //turn the data from the json request into a gigantic dictionary
options:0
error:&error];
NSDictionary* route = [json objectForKey:#"route"]; //created a dictionary out of the contents of route
NSDictionary* shape = [route objectForKey:#"shape"]; //create a sub-dictionary with the contents of shape
NSArray* shapePoints = [shape objectForKey:#"shapePoints"]; //turn shapePoints object into an NSArray
//Loop to turn the array of coordinate strings into CLLocation array
NSMutableArray* locations = [[NSMutableArray alloc] init];
NSUInteger i;
for (i = 0; i < ([shapePoints count])/2 ; i = i+2)
{
[locations addObject: [[CLLocation alloc]
initWithLatitude:[[shapePoints objectAtIndex:i] floatValue]
longitude:[[shapePoints objectAtIndex:i+1] floatValue]
]];
}
//When I NSLog within the function, the array has the correct data. But in viewDidLoad
//the array is null
[self.coordinates initWithArray:locations copyItems:YES];
}
How come this array become's null in viewDidLoad?
You are using GCD to make asynchronous request to fetch the data from viewDidLoad method. As it is asynchronous, it will not block the viewDidLoad method. The array gets populated once the async request downloads and gets parsed. That is the reason your array is nil in viewDidLoad.
If your UI looks blank until the data downloads and populates the array, you can choose to show an activity indicator. This will give the app user an idea that some activity is going on.
Hope that helps!
Probably what you need to do is wait for the process to get completed and the block literally doesn't know what your doing inside it or i would say the inside function is not been captured by the block.so use the array(call the function which is using your array) only after block gets completed.
- (void)fetchedData:(NSData *)responseData
{
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //turn the data from the json request into a gigantic dictionary
options:0
error:&error];
NSDictionary* route = [json objectForKey:#"route"]; //created a dictionary out of the contents of route
NSDictionary* shape = [route objectForKey:#"shape"]; //create a sub-dictionary with the contents of shape
NSArray* shapePoints = [shape objectForKey:#"shapePoints"]; //turn shapePoints object into an NSArray
//Loop to turn the array of coordinate strings into CLLocation array
NSMutableArray* locations = [[NSMutableArray alloc] init];
NSUInteger i;
for (i = 0; i < ([shapePoints count])/2 ; i = i+2)
{
[locations addObject: [[CLLocation alloc]
initWithLatitude:[[shapePoints objectAtIndex:i] floatValue]
longitude:[[shapePoints objectAtIndex:i+1] floatValue]
]];
}
//When I NSLog within the function, the array has the correct data. But in viewDidLoad
//the array is null
[self.coordinates initWithArray:locations copyItems:YES];
//Here you call the methods which is accessing your self.coordinates array.
NSLog(#"%#", self.coordinates);
}
Related
Response:
{"rsBody":
[{"productId":11,
"productImageUrl":"http:xxxx"},
{"productId":9,
"productImageUrl":"http:"xxxx"}]}
I know this is a repeated question, but still asking cause not getting the right way to do it. I am getting some response from php server as JSON in an array which consists two objects. I want to map the element of both objects productImageUrl in an NSArray. Resultant array should be somewhat like
NSArray =[{#"url":"productImageUrl1"},{#"url":#"ProductImageUrl2"}, nil];
productImageUrl1 = element of 1st object, productImageUrl2 = element of 2nd object.
I am parsing the response and able to to extract it from rsBody.
NSDictionary* response=(NSDictionary*)[NSJSONSerialization
JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSArray *rsBody = [response objectForKey:#"rsBody"];
Try this:
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary* response = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSArray *rsBody = [response objectForKey:#"rsBody"];
for (NSDictionary *dict in rsBody)
{
NSMutableDictionary *dictURL = [[NSMutableDictionary alloc] init];
[dictURL setValue:[dict valueForKey:#"productImageUrl"] forKey:#"url"];
[arr addObject:dictURL];
}
NSLog(#"%#", arr);
i am newBie in iOS Development. i want to add my JSON Parsing Data Dictionary Array Key Value in to another array But it only Add My Last Array index in to New Array.
My Code like as
-(void)fetchedData:(NSData *)responsedata
{
if (responsedata.length > 0)
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[_json objectForKey:#"data"] isKindOfClass:[NSArray class]])
{
NSArray *arr = (NSArray *)[_json objectForKey:#"data"];
[self.imageArray addObjectsFromArray:arr];
[self.storeViewTable reloadData];
}
self.storeViewTable.hidden=FALSE;
}
NSMutableArray *imagearray=[[NSMutableArray alloc]init];
for (index=0; index <[self.imageArray count]; index ++)
{
for(NSDictionary *dict in self.imageArray )
{
imagearray = [dict valueForKey:#"demopage"];
self.imagesa = imagearray;
}
}
NSLog(#"Array Count %d",[self.imagesa count]);
NSLog(#"Another array Count %d",[self.imageArray count]);
}
Here self.imagearray is my main array that contain my all JSON Data but i want to Parse A new Data From old Data And add it in to self.imagesa array here For self-images Value for key is demo page and Self.imagearray count is Three(3) So i want all my Three Index value in to self.imagesa but it contain only Last index Value please Give me Solution For that.And my Webservices link is Here. link
your code should look like below.
id json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[json objectForKey:#"data"] isKindOfClass:[NSArray class]])
{
NSArray *arr = (NSArray *)[json objectForKey:#"data"];
[imageArray addObjectsFromArray:arr];
}
//do not alloc init if you have already alloc init array.
NSMutableArray *imagesa=[[NSMutableArray alloc]init];
for(NSDictionary *dict in imageArray )
{
[imagesa addObject:#{#"demopage":[dict valueForKey:#"demopage"]}];
}
NSLog(#"Array Count %lu",(unsigned long)[imagesa count]);
NSLog(#"Another array Count %lu",(unsigned long)[imageArray count]);
and i got below output
Array Count 3
Another array Count 3
NOTE
and if you want to add all demopage dictionary to imagesa array then replace your for loop
for(NSDictionary *dict in imageArray )
{
NSArray *arr = (NSArray *)[dict valueForKey:#"demopage"];
[imagesa addObjectsFromArray:arr];
}
and output is
Array Count 69
Another array Count 3
I have this code, but when I log the mediaDictionaryArray, I get null. Does the receiver array have to be initialized with a value first or can I add objects to an empty array? Does [NSArray array] vs. [[NSArray alloc]init] have anything to do with it?
Adding dictionary from API call that happens i times. Asynch call will return the dictionary - can't be sure if NSMutableArray will work in catchJSONArray since asynch nature of call will make the array of indeterminate size which will make it hard to use later on.
Updated with relevant bit.
for (int i = 0; i<[array count]; i++) {
NSString *getString = array[i];
NSLog(#"getstring %#", getString);
[client GET:getString parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
_locationMediaArray = (NSArray*)responseObject[#"data"];
[self catchJSONArray:_locationMediaArray];
then here is method with the array issue
-(void)catchJSONArray:(NSArray*)array{
NSArray *catchJSONArray = [NSArray array];
_mediaDictionaryArray = [catchJSONArray arrayByAddingObjectsFromArray:array];
NSLog(#"mediaDictionaryArray %#", _mediaDictionaryArray);
}
arrayByAddingObjectsFromArray returns a new array containing your objects, as an NSArray can not be changed once created.
If you want to change an existing array, you should be using an NSMutableArray.
The best way you could do this is:
_mediaDictionaryArray=[NSArray arrayWithArray:otherArray];
That will create a new array with the contents of otherArray and assign it to _mediaDictionary.
No need to alloc init your array just pass the refrence of your other array. If
_mediaDictionaryArray is mutable array then use below:-
_mediaDictionaryArray=[array mutableCopy];
If it is non mutable array then use below
_mediaDictionaryArray=[array copy];
When I try to print array of json values in log, I get addresses instead of values. Here's how I coded.
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:jsonArray.count];
NSMutableArray *anotherTempArray = [NSMutableArray arrayWithCapacity:jsonArray.count];
NSDictionary *dict;
for(dict in jsonArray)
{
NSString *projectName = dict[#"Name"];
NSString *urlText = dict[#"Url"];
NSLog(#"Url text in array = %#", urlText);
NSString *attch = dict[#"attachmentes"];
NSLog(#"Attached url in array = %#", attch);
NSString *projID = dict[#"ProjectID"];
NSLog(#"Project ID in array = %#", projID);
SaveAttachment *saveAt = [[SaveAttachment alloc] initWithName:projectName withList:#"View" withAttachment:#"View"];
[tempArray addObject:saveAt];
SaveProjectId *saveProj = [[SaveProjectId alloc] initWithProjectId:projID];
saveProj.projectId = projID;
[anotherTempArray addObject:saveProj];
}
array = tempArray;
[self.tableViewProject reloadData];
NSLog(#"Array of project IDs === %#", anotherTempArray); //Get values (array of project ids here.
}
Replace
SaveProjectId *saveProj = [[SaveProjectId alloc] initWithProjectId:projID];
saveProj.projectId = projID;
[anotherTempArray addObject:saveProj];
with
[anotherTempArray addObject:projID];
This is because your anotherTempArray contains objects of SaveProjectId ie, everytime in for loop you are adding saveProj object not projID. Thats why your array showing SaveProjectId objects.
If you want to directly save them, then use the below modification
[anotherTempArray addObject:projID];
or you can use like(this is i would prefer)
NSLog(#"First project ID === %#", [anotherTempArray objectAtindex:0] projectId]);
You are storing SaveProjectId objects in the array, therefore when you print the content you see the address of those objects.
your "anotherTemoArray" is having objects of SaveProbectId so you have to pass object at index to SaveProjectId and then you can see the array information
When calling NSLog(#"Array of project IDs === %#", anotherTempArray); the -(NSString*)description method on each of the objects inside 'anotherTempArray' is being called.
In your case that means -(NSString*)description is being called on SaveProjectId objects. Override it to print out what you want... e.g.
-(NSString*)description {
return [NSString stringWithFormat:#"SaveProjectId: %#",self.projectId];
}
Here is the situation:
I have a request on AFNetworking that retrieves me a JSON with an NSArray.
My goal is to mutate the NSDictionaries inside it. I already made a mutableCopy of the array, but I want to know if I can easily mutate all the content. Will I have to iterate through the array manually?
NSJSONSerialization has options to allow you to control the mutability of the resulting data structure. Just pass the appropriate ones (probably NSJSONReadingMutableContainers) and there you go.
You cannot mutate NSDictionary, just because only NSMutableDictionary has method setObject:forKey:
So you should create mutableCopy of each dictionary and empty mutable array. Then with a forloop fill that array. Your code should be so:
- (NSMutableArray *)mutatedArrayFromArray:(NSArray *)array
{
NSMutableArray *resultArray = [NSMutableArray new];
if([array count] > 0)
{
for(int i = 0; i < count; i++)
{
NSMutableDictionary *mutatedItem = [[array objectAtIndex:i] mutableCopy];
[resultArray addObject:mutatedItem];
[mutatedItem release]; // only with ARC disabled
}
}
return [result autorelease]; // if ARC enabled : return result;
}