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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:59:04+00:00 2026-06-03T14:59:04+00:00

I am trying to add an object of the same class to a NSMutable

  • 0

I am trying to add an object of the same class to a NSMutable array that is defined in the same class.
My approach would look like this, but when I tested the array, it was always empty. I also tried adding random Strings or integers.

The .h file:

@property NSMutableArray *anArray;
- (void)addObject: (TheClass*)nameOfObjectToBeAdded;

The .m file:

@synthesize anArray;

- (void)addObject: (TheClass*)nameOfObjectToBeAdded {
    [anArray addObject:nameOfObjectToBeAdded];
}

The main.m:

#import "TheClass.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {

        TheClass *object1 = [[TheClass alloc] init];
        TheClass *object2 = [[TheClass alloc] init];
        [object1 addObject:object2];

    }
    return 0;
}

I am trying to add object2 to the NSMutableArray of object1.

Thanks in advance!

  • 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-03T14:59:05+00:00Added an answer on June 3, 2026 at 2:59 pm

    You need to allocate and initialize the array before using it:

    In Class.m, probably in init:

    self.anArray = [[NSMutableArray alloc] init];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to add a class object into a List using reflection, but when invoking
I have a source object that looks like this: private class SourceObject { public
I am trying to add time to a Date object in javascript but am
I'm trying to add a unittest attribute to an object in Python class Boy:
I'm trying to add plugins to my program, and this looks good, except that
This might not be possible but I am trying to create a constructor that
I have a object structure like this: public class Entity { IList<Relationship> Relationships{get;set;} }
I'm trying to create a class that will allow me to add/multiply/divide objects of
I don't know why this is erroring, but I'm just trying to add something
I am trying to add the datetime object of a person. Whenever the birth

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.