Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:09:57+00:00 2026-05-26T02:09:57+00:00

I believe in learning along the process and the following code segment is not

  • 0

I believe in learning along the process and the following code segment is not very clear to me. I know that an alloc statement would increase the retain count however there are certain aspects of iOS development that are still very confusing to me.

Why do I need a : [jokesArray retain]; in the following code segment?

I have a jokesArray = [[NSArray alloc]init]; and from what I have read is enough to retain?

Can someone please explain in an easy to understand manner why that retain statement is needed? (Else the app crashes with a EXC_Bad_Access.

I have had some kind people try to explain but it has not worked. Any help will be greatly appreciated.

#import "JokesViewController.h"


@implementation JokesViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{

    [super viewDidLoad];

    jokesArray = [[NSArray alloc]init];

    [self getJokes];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [jokesArray release];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [jokesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }





[[cell textLabel]setText:[[jokesArray objectAtIndex:0]objectForKey:@"text"]];    

 //   [[cell textLabel]setText:@"ok"];   

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
}


#pragma mark - Custom Functions

-(void) getJokes
{
    NSURL *url = [NSURL URLWithString:@"someurl"];
    __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setCompletionBlock:^{
        // Use when fetching text data
        NSString *responseString = [request responseString];

        NSDictionary *resultsDictionary = [responseString objectFromJSONString];
        jokesArray = [resultsDictionary allValues];

        [jokesArray retain];  //WHY DO I NEED THIS?


        [self.tableView reloadData];

        NSLog(@"%@", [jokesArray description]);

        // Use when fetching binary data
  //      NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];

    }];
    [request startAsynchronous];
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:09:57+00:00Added an answer on May 26, 2026 at 2:09 am

    You are leaking your previous NSArray allocation because in this part of the code:

     NSDictionary *resultsDictionary = [responseString objectFromJSONString];
        jokesArray = [resultsDictionary allValues];
    
        [jokesArray retain];
    

    You are creating an NSDictionary and replacing whatever jokesArray was pointing to point to the data from NSDictionary that you just created.
    Also the data returned by NSDictionary was created using a convenience initialization method which means it will get released after a while thus the reason why you need to retain.

    Since you are modifying the jokesArray variable directly the previously allocated NSArray doesn’t get released when you replace it with the new object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe the following VB.Net code is the equivalent of the proceeding C# code;
After learning more about how monotouch works, I believe I know the answer to
Just experimenting and learning, and I know how to create a shared dictionary that
I believe the application has some parts that target .NET, and some that don't.
I believe several of us have already worked on a project where not only
I believe that Agile is nothing but another implementation of Spiral Model. I am
I believe that quantifying the productivity increase (extra working hours) is the most effective
I'm finally learning regexps and training with ack . I believe this uses Perl
I'm learning sml, and wrote the following simple function: (* Return a list with
I am quickly learning the ins and outs of database design (something that, as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.