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 8405321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:49:29+00:00 2026-06-09T22:49:29+00:00

I am trying to populate an NSMutableArray here named data with NSArrays. When I

  • 0

I am trying to populate an NSMutableArray here named “data” with NSArrays. When I try to retrieve I get EXC_BAD_ACCESS. Here is my code for poulating

.h

@property (nonatomic, retain) NSMutableArray* data;

.m

@synthesize data;

in viewDidLoad

self.data = [NSMutableArray array];
NSArray* ar1 = [[NSArray arrayWithObjects: @"text1", @"text2", @"text3", @"text4", nil] autorelease];
[self.data addObject:ar1];

Now in other method I am trying to get the inner NSArray back:

NSArray* sItem = [NSArray array]; 
sItem = (NSArray*)[self.data objectAtIndex:0];

if (sItem)
{
    if([sItem isKindOfClass:[NSArray class]])/////ERROR LINE
    {
        NSLog(@"Its an Array.");
    }
}
  • 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-06-09T22:49:30+00:00Added an answer on June 9, 2026 at 10:49 pm

    In -viewDidLoad, you are over-releasing ar1. It is returned already autoreleased. You know this because you obtained the array via arrayWithObjects:, not by alloc/init.

    The pattern is:

    NSArray * temporaryArray = [ [ [ NSArray alloc ] initWithObjects:obj_0, ..., obj_n, nil ] autorelease ]
    

    or

    NSArray * temporaryArray = [ NSArray arrayWithObjects:obj0, ..., obj_n, nil ] ;
    

    You should switch your project to ARC–it will handle this for you.

    EDIT:

    You can also rewrite

    NSArray* sItem = [NSArray array]; 
    sItem = (NSArray*)[self.data objectAtIndex:0];
    if (sItem) {
        if([sItem isKindOfClass:[NSArray class]]){ /////ERROR LINE
             NSLog(@"Its an Array.");
        }
    

    as

    NSArray* sItem = nil ;
    sItem = [self.data objectAtIndex:0];
    if( [ sItem isKindOfClass:[ NSArray class ] ] )
    {
        NSLog(@"Its an Array.");
    }
    

    or even

    NSArray* sItem = [self.data objectAtIndex:0];
    if( [ sItem isKindOfClass:[ NSArray class ] ] )
    {
        NSLog(@"Its an Array.");
    }
    

    Notes about your code:

    1. You are allocating a new NSArray and assigning it to the pointer sItem. This array is immediately discarded when you assign another value to sItem. Just initialize sItem to nil instead.
    2. You don’t need to check if (sItem)… Sending -isKindOfClass: (or any message in fact) to nil will always return NO/nil/0.

    Finally, I often check for the presence of an array by doing something like:

    if ( sItem.length > 0 ) { /* yes, valid, non-empty array */ }
    

    This shortcut is especially good for strings.

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

Sidebar

Related Questions

I am trying to populate a spinner, but am getting an error. Here is
I'm trying to populate a dictionary dynamically in a for loop like so: pseudo-code
I'm trying to populate this list with strings from the object data. It comes
I am trying to populate variables in the Report Custom Code area (or even
I am trying to populate the tableview with an NSMutableArray, and I am having
iam trying to populate the HyperlinkButton.NavigateUri Property with a string from the properties... But
Im trying to populate a JSF Datatable with data that I am retrieving from
I'm trying to populate a smartgwt calendar using data form a server obtained using
I have a NSMutableArray property declared as (nonatomic, retain) called Categories. In dealloc, I
I'm trying to populate a simple uitableview in XCode 4.1. Here is MessagesInbox.h: #import

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.