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

  • Home
  • SEARCH
  • 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 3798106
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:31:03+00:00 2026-05-19T13:31:03+00:00

Hey! I’ve been trying to add an array to an NSMutableArray using the addObject

  • 0

Hey! I’ve been trying to add an array to an NSMutableArray using the addObject syntax. The app crashes without any explicit mention of an error. Could you please tell me how this is done?


I have a class Stack that creates an array. So I call that class using an instance called tem that I have created. Hence, [self tem] is my call to the array. Through the program I merely add UILabels to the array(I know you’d suggest adding a string and then changing to UILabels, but I need to do it this way) and towards the end, I’d like to add this array to my ‘list’ array.

-(Stack *)tem {
  if(!tem)
    tem=[[Stack alloc]init];
  return tem;
}

-(void)viewDidLoad{
  //code
  list=[NSMutableArray arrayWithArray:[self list].array];
}

-(IBAction)opPr:(UIButton *)sender
{
  if([[[sender titleLabel]text] compare: @"="]==0) {
    //code
    UILabel *t=[[UILabel alloc]init];
    //complete creating label
    [[self tem]push:t];
    //add above label to tem array
    [list addObject:[self tem].array];
    [table reloadData];
  }
}
  • 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-19T13:31:03+00:00Added an answer on May 19, 2026 at 1:31 pm

    OK, this answer got all cluttered with edits. I’ve edited it to be more clear, at the possible expense of understanding the thread of how we arrived at the final answer.

    The final answer

    The answer was to change:

    list=[NSMutableArray arrayWithArray:[self list].array];
    

    To:

    list = [[NSMutableArray alloc] init];
    

    (…Which isn’t really the best way to create an NSMutableArray, but it may work anyway.)

    This was based on an erroneous version of the asker’s code

    Based on your comment that it crashes after [list addObject:[self tem].array];, I must conclude that your instance variable list is of type Stack*, and that Stack is not a subclass of NSMutableArray. If so, that’s your issue.

    In that case, changing that line to [[list array] addObject:[self tem].array]; should fix it.

    This is just good advice

    As an aside, NSMutable array is perfectly capable of acting as a stack without modification. Example:

    NSMutableArray* ar = [NSMutableArray arrayWithCapacity:someCapacity];
    // Push
    [ar addObject:narf]; // narf being some object reference
    // Pop
    id popped = [ar lastObject];
    [ar removeLastObject];
    

    If you want to encapsulate stack behavior in a semantically consistent way, you can add push and pop methods to NSMutableArray using a category. This would make your code simpler and less prone to error.

    This was my first stab at answering the question, before any code had been posted

    This is a stab in the dark since you’ve not posted any code. BUT. One way to accomplish that with arrays is by creating arrays using [NSArray arrayWithObjects:obj obj ... nil] and omitting the nil terminator on the list of objects. Are you by any chance doing that?

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

Sidebar

Related Questions

No related questions found

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.