EXC_BAD_ACCESS Error with NSDictionary - ios

When I fetch data from a JSON file I get an error -> EXC_BAD_ACCESS .
I am not using ARC, here's my code:
-(void)fetchedData:(NSData*)responseData
{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
fetchedResutsArray = [json objectForKey:#"People"];
NSLog(#"%#", fetchedResutsArray);
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSDictionary *resultsDict = [fetchedResutsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [resultsDict objectForKey:#"prename"];
return cell;
}
This is my JSON-Result. I get the data from a website
JSON:
{
prename = "Tim";
lastname = "Test";
username = "tim23";
},
{
prename = "John";
lastname = "Test";
username = "johniii";
},
{
prename = "Peter";
lastname = "Test";
username = "tenek23";
}
Xcode is showing this in NSLOG.

fetchedResutsArray = [json objectForKey:#"People"];
This creates an autoreleased object which will be released sooner or later. You must not access this object outside of that method. You are violating memory management rules.
You can retain the object, which should solve your problem.
[fetchedResutsArray release]; // release old instance
fetchedResutsArray = [[json objectForKey:#"People"] retain];
Or use a retain #property and assign the object by using self.fetchedResutsArray = [json objectForKey:#"People"];.
Don't forget to release the object in dealloc
Or just use ARC.

Related

How to bind NSDictionary data to table view in objective c?

I have used the below code to convert the JSON data(from SOAP service) to NSDictionary.
-(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
NSLog(#"data: %#",_data);
NSArray *value = [_data valueForKey:#"GetDemoDataResult"];
NSError *error;
NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"%#",json);
}
Output
2017-04-04 13:03:51.085 SoapService[18444:588594] (
{
firstName = "1 Jhon";
lastName = Macay;
},
{
firstName = "2 William";
lastName = Proter;
},
{
firstName = "3 Joe";
lastName = Root;
},
{
firstName = "4 Brendon";
lastName = Haejoy;
},
{
firstName = "5 Jhon";
lastName = Snow;
},
{
firstName = "6 Theon";
lastName = Greyjoy;
}
)
Do I need to convert this to any other? or how could I bind the above output in UITable​View?
To work with table view you need array
Checkout this simple table view tutorial
It should be like this
Declare jsonArray in your .h file
#property (strong, nonatomic) NSMutableArray *jsonArray;
Add below line viewDidLoad
self.jsonArray = [[NSMutableArray alloc]init];
-(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
NSLog(#"data: %#",_data);
NSArray *value = [_data valueForKey:#"GetDemoDataResult"];
NSError *error;
NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"%#",son);
[self.jsonArray addObject:[[json objectForKey:#"firstname"]stringByAppendingString:[json objectForKey:#"lastname"]];
[tableViewName reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.jsonArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [self.jsonArray objectAtIndex:indexPath.row];
return cell;
}
take one NSMutuableArray and add dictionary to this array like
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: json];
//Than Reload Tableview
Note: Declare array Globally to access in your class
tableview display data like
cell.label.text = [[array objectAtIndex:indexPath.row]valueForKey:#"firstName"];
Store json data into Global Declare NSArray.
-(void)retriveFromSYSoapTool:(NSMutableArray *)_data{
NSLog(#"data: %#",_data);
NSArray *value = [_data valueForKey:#"GetDemoDataResult"];
NSError *error;
NSData *objData = [value[0] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"%#",json);
DataArray = [NSArray arrayWithObjects:json, nil];
[tableView reloadData];
}
Here DataArray is Globally Declare NSArray Object;
Now Write UITableView DataSource Method.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return DataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"jsoncell" forIndexPath:indexPath];
NSDictionary *dict = DataArray[indexPath.row];
NSString *output = [NSString stringWithFormat:#"%# %#",dict[#"firstName"],dict[#"lastName"]];
cell.textLabel.text = output;
return cell;
}

How do you load multiple arrays into a UITableView?

I am calling some JSON and loading a table with the data from a single array. That's working great. Now I'm trying to figure out
A. the best way to load the data into the table and
B. the best way to section that data off.
This is my 6th week of iOS development and I am pretty new. I have a fairly weak Javascript background.
My first (failed attempt) way to concatenate the arrays together and pass that to the tableview. I think this is wrong for multiple reasons (issues with sectioning afterwards, know "which" one to delete, etc). Any help is greatly appreciated!
Didn't work:
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&errorJson];
self.allGroups = [dataDictionary objectForKey:#"all_groups"]; //NSDictionary
self.firstGroup = [self.allGroups objectForKey:#"first_group"]; //NSMutableArray
self.secondGroup = [self.allGroups objectForKey:#"second_group"]; //NSMutableArray
self.thirdGroup = [self.allGroups objectForKey:#"third_group"]; //NSMutableArray
NSMutableArray *allGroupsArray = [self.firstGroup arrayByAddingObjectsInArray:[self.secondGroup arrayByAddingObjectsInArray:self.thirdGroup]];
Does work now, but can't figure out multiple arrays into the tableview:
-(void) getTheData {
NSString *sessionToken = [[AFOAuthCredential retrieveCredentialWithIdentifier:#"myToken"] accessToken];
if (sessionToken == nil) {
LoginViewController *loginView = [[LoginViewController alloc] init];
[self presentViewController:loginView animated:NO completion:nil];
return;
}
NSURL *url = [NSURL URLWithString:#"https://greatwebsitetogetdata.com"];
AFOAuth2Client *oauthClient = [AFOAuth2Client clientWithBaseURL:url clientID:#"MY_CLIENT" secret:#"1234567890abc"];
[oauthClient getPath:#"/api/v1/json" parameters:#{#"access_token": sessionToken} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *errorJson = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&errorJson];
self.allGroups = [dataDictionary objectForKey:#"all_groups"]; //This is a NSDictionary
self.firstGroup = [self.allGroups objectForKey:#"first_group"]; //This is a NSMutableArray
self.secondGroup = [self.allGroups objectForKey:#"second_group"]; //This is a NSMutableArray
self.thirdGroup = [self.allGroups objectForKey:#"third_group"]; //This is a NSMutableArray
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.firstGroup.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *groupInfo = self.firstGroup[indexPath.row];
static NSString *cellIdentifier = #"Cell";
groupTableViewCell *cell = (groupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[groupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.groupTitle.text= groupInfo[#"title"];
cell.groupLikes.text= [NSString stringWithFormat:#"%#", groupInfo[#"likes"]];
cell.groupRunDates.text= [NSString stringWithFormat:#"%# - %#", groupInfo[#"start_date"], groupInfo[#"end_date"]];
cell.groupAcceptance.text= groupInfo[#"acceptance_type"];
return cell;
}
I think an array of arrays would work better for you, where each array represents a section. allGroups should then contain 3 arrays.
Then you need to override the datasource method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.allGroups.count;
}
and then in:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allGroups[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *group = self.allGroups[indexPath.section];
NSDictionary *groupInfo = group[indexPath.row];
static NSString *cellIdentifier = #"Cell";
groupTableViewCell *cell = (groupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[groupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.groupTitle.text= groupInfo[#"title"];
cell.groupLikes.text= [NSString stringWithFormat:#"%#", groupInfo[#"likes"]];
cell.groupRunDates.text= [NSString stringWithFormat:#"%# - %#", groupInfo[#"start_date"], groupInfo[#"end_date"]];
cell.groupAcceptance.text= groupInfo[#"acceptance_type"];
return cell;
}

Parse JSON correctly for table view cell

I'm parsing JSON from my API which works fine but every time I select a row and get to the detail view he shows me the product with is supposed to be linked to the product shown at the very bottom. I don't understand why it isn't parsing the product id for the specific row because I have set it to parse for indexPath.row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *neuheitenCell = #"neuheitenCell";
CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];
wareID = [[arrayArtikelWareID objectAtIndex:indexPath.row] objectForKey:#"ware_id"];
NSLog(#"Waren ID: %#", wareID);
cell.artikelName.text = [[arrayArtikelName objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.artikelHersteller.text = [[arrayArtikelHersteller objectAtIndex:indexPath.row] objectForKey:#"lieferant_name"];
return cell;
}
**EDIT:
-(void) parseJSONWithURL: (NSURL *) jsonURL {
dispatch_async(mainThreadQueue, ^{
NSError *error = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *json =[NSString stringWithContentsOfURL:jsonURL encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
dictionaryNewStuff = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
if (error == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
arrayNeuheiten = [[dictionaryNewStuff valueForKey:#"newstuff"] valueForKey:#"Neuheiten"];
arrayArtikelBild = [[dictionaryNewStuff valueForKey:#"newstuff"] valueForKey:#"Neuheiten"];
[neuheitenTableView reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
}
}
});
}
JSON can be viewed at my API
Does anyone know how to fix this?
Thanks in advance!
Could you try using following code ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *neuheitenCell = #"neuheitenCell";
CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];
NSDictionary *object = [arrayNeuheiten objectAtIndex:indexPath.row];
NSString *wareID = [object objectForKey:#"ware_id"];
NSLog(#"Waren ID: %#", wareID);
cell.artikelName.text = [object objectForKey:#"name"];
cell.artikelHersteller.text = [object objectForKey:#"lieferant_name"];
return cell;
}
Why do you need two arrays for holding the data ? I mean following lines " arrayNeuheiten = [[dictionaryNewStuff valueForKey:#"newstuff"] valueForKey:#"Neuheiten"]; arrayArtikelBild = [[dictionaryNewStuff valueForKey:#"newstuff"] valueForKey:#"Neuheiten"]; "
Also where do you assign anything to arrayArtikelWareID ,arrayArtikelName ,arrayArtikelHersteller

Does assigning an autorelease object to a retained property increase its retain count?

I would have thought 'self.data=' would retain the autorelease NSMutableArray objects and the NSMutableDictionary objects it contains, but eventually I get EXC_BAD_ACCESS when the table's cellForRowAtIndexPath method tries to access the NSDictionaries in self.data.
#property (strong, nonatomic) NSMutableArray *data;
- (void) updateReceivedData:(NSData *) jsonData
{
NSMutableArray *fetchedData = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
self.data = [self convertDates:fetchedData withFormat:kMySQLDateTimeFormat];
[self.tableView reloadData];
}
}
- (NSMutableArray*) convertDates:(NSMutableArray *) array withFormat:(NSString *) format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
NSMutableArray *newArray = [NSMutableArray arrayWithArray:array];
for (NSMutableDictionary *dict in newArray)
{
for (id key in dict.allKeys)
{
if ([[dict objectForKey:key] isKindOfClass:[NSString class]])
{
NSString *value = [dict objectForKey:key];
NSDate *date = [dateFormatter dateFromString:value];
if (date) [dict setObject:date forKey:key];
}
}
}
[dateFormatter release];
return newArray;
}
BAD_ACCESS HERE thrown here between the NSLogs.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog (#"Cell was nil");
cell = [[[CustomCell alloc] init] autorelease];
}
NSDictionary *dict = [[NSDictionary alloc] init];
if (_isFiltered){
dict = [self.filteredData objectAtIndex:indexPath.row];
} else {
dict = [self.data objectAtIndex:indexPath.row];
}
NSLog (#"Filling Label 1");
cell.IDLabel.text = [[dict objectForKey:#"Id"] stringValue];
NSLog (#"Filling Label 2");
cell.firstNameLabel.text = [dict objectForKey:#"firstName"];
[dict release];
return cell;
}
Turn on zombies and see if it catches the problem (EXC_BAD_ACCESS does not necessarily mean an over-released object, but it might).
What happens to the absolute value of the retain count of an object is irrelevant.
However, a strong property implies that the object is retained, yes, if you assign through the setter (i.e. self.data = ... and not _data = ...).
Why are you releasing the dict in cellForRowAtIndexPath: . Eventhough you allocate dict, you are assigning another pointer which is an object from filteredData or data. Just remove the [data release] and while declaring data assign it as nil
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog (#"Cell was nil");
cell = [[[CustomCell alloc] init] autorelease];
}
// **As you will be assigning the object from filteredData/data, don't allocate here**
NSDictionary *dict = nil;
if (_isFiltered){
dict = [self.filteredData objectAtIndex:indexPath.row];
} else {
dict = [self.data objectAtIndex:indexPath.row];
}
NSLog (#"Filling Label 1");
cell.IDLabel.text = [[dict objectForKey:#"Id"] stringValue];
NSLog (#"Filling Label 2");
cell.firstNameLabel.text = [dict objectForKey:#"firstName"];
// **Release not required as you didn't allocate**
//[dict release];
return cell;
}

Count number of values in dictionary object

<pre>
products = (
{
id = 19;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330374078photography.jpg";
name = "Save $240 on your next photo session";
},
{
id = 21;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330373696massage.jpg";
name = "One Hour Massage";
}
);
}
</pre>
the above is what I got through json, I need to assign the values to uitableviews:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(#"Number of rows: %d",[[products objectForKey:#"products"] count]);
return [[products objectForKey:#"products"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
}
NSString *currentProductName;
currentProductName = [productKeys objectAtIndex:indexPath.row];
NSLog(#"product name : %#", currentProductName);
[[cell textLabel] setText:currentProductName];
return cell;
}
it returns 0 number of rows, I am newbie to ios please help me how I will assign these values to uitableview.
Regards
The problem is that what you posted is not json. It should be
{
"products":[
{
"id":19,
"image_url":"http://localhost:8888/straightoffer/image/data/1330374078photography.jpg",
"name":"Save $240 on your next photo session"
},
{
"id":21,
"image_url":"http://localhost:8888/straightoffer/image/data/1330373696massage.jpg",
"name":"One Hour Massage"
}
]
}
You can use http://jsonlint.com/ to validate your json files.
I have used the above json file and did the following:
NSBundle* bundle = [NSBundle mainBundle];
NSString *jsonPath = [bundle pathForResource:#"data" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error;
NSDictionary *products = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
DLog(#"products: %i", [[products objectForKey:#"products"] count] );
[self.tableView reloadData];
Result is: products: 2.
You should be able to reproduce this.

Resources