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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:55:32+00:00 2026-06-03T18:55:32+00:00

I come from a Java background and I am learning Objective C. I am

  • 0

I come from a Java background and I am learning Objective C. I am trying to create a class that has a string array and a member function to modify the Array. My code looked like this:

@implementation TAWChapter

@synthesize mSubject;

@synthesize mItems;

- (id) init{
    self.mItems = [[NSMutableArray alloc] init];
    return self; 
}

- (void) setSubject:(NSString *)subject{
    self.mSubject = subject; 
}

- (void) addItem:(NSString *)item{
    [self.mItems addObject:@"asdf"]; 
}

@end

Which didn’t work. I got a "[__NSArrayI addObject:]: unrecognized selector sent to instance " and a "NSInvalidArgumentException". After searching on internet, I changed the single line in the constructor to:

self.mItems = [self.mItems init];

It worked, but why? From a Java developer’s point of view the first one makes more sense than the second one. And I have another line it’s the same as the first one but it’s working(not in a constructor). Can someone explain this to me please?

  • 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-03T18:55:35+00:00Added an answer on June 3, 2026 at 6:55 pm
    1. First of all, you should adhere to Objective-C coding conventions. In Objective-C, classes don’t have constructors, they have initialisers. In Objective-C, initialisers invoke the initialiser of the superclass, so it should look like this:

      - init
      {
          self = [super init];
          if (!self) return nil;
      
          // set up other stuff here 
      
          return self;
      }
      
    2. Second, unless you are using ARC, you might have a memory leak. The first line of your initialiser assigns an object that you own to a property that also likely takes ownership. You should use either:

      // property takes care of ownership
      self.mItems = [NSMutableArray array];
      

      or:

      // assign to instance variable directly with owned object
      mItems = [[NSMutableArray alloc] init];
      

      Apple sometimes discourage the use of accessor methods in initialisers because it can fiddle with things like KVO, but consistent use of accessor methods ensures proper object ownership and memory management.

    3. By changing your line in your initialiser to:

      self.mItems = [self.mItems init];
      

      does nothing. When your initialiser method is called (which is typically just after it has been allocated), all instance variables are automatically set to nil. So what you are doing is just:

      self.mItems = [nil init];
      

      which is just:

      self.mItems = nil;
      

      and, don’t use init without first allocating an instance, and never use init more than once.

    4. If you do not let the superclass initialise itself, then it may manifest as problems in other areas. Do a Build & Analyze and ensure you have fixed up any issues pointed out by the analyser.

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

Sidebar

Related Questions

So I come from a background in Java where you can create a class
I'm new to PHP but come from a Java background and I'm trying to
I come from Java background, where we have data structures with interfaces that if
I come from a Java/Eclipse background and I fear that I am spoiled by
I come from a Java/C++ OOP background and am trying to get into JavaScript
I come from a Java background and I am now trying to study PHP
I come from Java Background and so used to Debugging using Eclipse but have
I come from a Java background and have been using C# for the last
I've come from a java background, so i'm still getting to grips with some
I come from a Java SE background, and I did some servlets tutorials and

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.