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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:47:05+00:00 2026-05-17T06:47:05+00:00

Ok, I created my own class(MyObject) subclassing NSObject then in my code I declare

  • 0

Ok, I created my own class(MyObject) subclassing NSObject

then in my code I declare this:

MyObject * myObject;

Then further down in my function, I do:

if(myObject == nil)
{
 myObject = [self getObject];
}

My if statement returns false, which I want it to be true.

In debug mode: just declaring it is assigning an instance to it with some random values.
So, do I have to override the init function so it returns nil, and then create my own initWith function?

  • 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-17T06:47:06+00:00Added an answer on May 17, 2026 at 6:47 am

    In Objective-C, (or C in general),

     MyObject* myObject;
    

    inside a method implementation does not initialize myObject with a nil. Similarly,

     int a;
    

    does not initialize a with 0. That’s what people who made C decided long ago. There was a historical rational why this was so.

    Just initialize it explicitly as

     MyObject* myObject=nil;
    

    Note that an ivar defined in the class interface is automatically set to zero before init is called.

    Update: Also note that myObject is a pointer to the real object which contains data. So, if you just do

     MyObject* myObject;
    

    this means myObject points to a chunk of garbage memory, which would not correctly work at all.

     MyObject* myObject=nil;
    

    makes myObject to point to nothing. Now it at least consistently does nothing. What this line

     MyObject* myObject=[[MyObject alloc] init];
    

    does is to allocate a MyObject object in the memory, initialize it, and then make myObject point to the chunk of memory correctly allocated and initialized. Now, if the MyObject has the interface

    @interface MyObject:NSObject {
         NSString* string;
    }
    @end
    

    and if you define the init method,

    @implementation MyObject
    -(id)init {
          if(self=[super init]) {
               ... do something ...
          }
          return self;
    }
    

    after [super init] is successfully performed, Objective-C guarantees that the ivar string is set to nil, i.e. string points to nothing. But it is not that an NSString is allocated or initialized.

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

Sidebar

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.