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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:40:42+00:00 2026-05-31T15:40:42+00:00

I understand that in Objective C ‘instance variables are always initialized to 0 (or

  • 0

I understand that in Objective C ‘instance variables are always initialized to 0 (or nil, NULL, or false, depending on the exact data type)’. stackoverflow question

In the unit test2 below, why is the instance variable _willBeRipeBy in the orange instance of Fruit not set to nil? It fails at STAssertNil([orange willBeRipeBy],nil).

test2 passes if I create an init method to explicitatly set _willBeRipeBy to nil or if I rename test1 to test3 to change the execution order.

Creating apple in test1 seems to be effecting the memory that orange uses but why is Fruits init not resetting the instance variable to nil.

I am new to objective C, Thanks.

(using Xcode 4.3, iOS5.0 with Automatic Reference Counting on)

@interface Fruit : NSObject
- (NSDate *)getWillBeRipeBy;
- (void)setWillBeRipeBy:(NSDate *)ripeBy;
@end

@implementation Fruit

NSDate *_willBeRipeBy;

- (NSDate *)getWillBeRipeBy{
    return _willBeRipeBy;
}
- (void)setWillBeRipeBy:(NSDate *)ripeBy{
    _willBeRipeBy = ripeBy;
}
@end

@implementation TestIvarInitialisationTests

- (void)test1
{
    Fruit *apple = [[Fruit alloc] init];
    STAssertNil([apple getWillBeRipeBy],nil);
    NSDate * now = [NSDate date];
    [apple setWillBeRipeBy:now];
    STAssertEqualObjects([apple getWillBeRipeBy], now,nil);
}

- (void)test2
{
    Fruit *orange = [[Fruit alloc] init];
    STAssertNil([orange getWillBeRipeBy],nil);
}
@end
  • 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-31T15:40:43+00:00Added an answer on May 31, 2026 at 3:40 pm

    You have to declare the variable as an instance variable inside the header file or in {}-brackets after the @implementation directive.

    Your implementation is a global variable

    btw. you shouldn’t name methods like “get…” – this is against cocoa naming conventions. You should only use “get..” when you want to set a non-object type with unknown size, e.g.: CGRect rect; [userInfoDictionary getValue:&rect forKey:@"bla"]

    you can use properties (@property(strong) NSString *property and @synthesize property) to auto-implement instance variables along with getter and setter methods

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

Sidebar

Related Questions

I am new in objective c. I understand that -init() is an instance method
I understand that in Objective-C you declare an array in the header file and
So, I understand that Categories in Objective-C can be used to add methods to
In objective-c, I understand that you need to release anything you init/retain/copy. Do I
Consider this snippet of Objective-C: NSString *elementString = nil; if(elementText != NULL) { elementString
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that there are several ways to blend XNA and WPF within the
I understand that they are both supposed to be small, but what are the
I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
I understand that server-side validation is an absolute must to prevent malicious users (or

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.