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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:24:54+00:00 2026-05-14T20:24:54+00:00

Update – Many people are insisting I need to declare an iVar for the

  • 0

Update – Many people are insisting I need to declare an iVar for the property. Some are saying not so, as I am using Modern Runtime (64 bit). I can confirm that I have been successfully using @property without iVars for months now. Therefore, I think the ‘correct’ answer is an explanation as to why on 64bit I suddenly have to explicitly declare the iVar when (and only when) i’m going to access it from a child class. The only one I’ve seen so far is a possible GCC bug (thanks Yuji). Not so simple after all… To clarify the possible bug is this: When inheriting from a base class, a child can not access the parent’s iVar IF that child also happens to implement an UNRELATED accessor using @synthesize BEFORE the iVar is accessed.

I’ve been scratching my head with this for a couple of hours – I haven’t used inheritance much.

Here I have set up a simple Test B class that inherits from Test A, where an ivar is declared. But I get the compilation error that the variable is undeclared. This only happens when I add the property and synthesize declarations – works fine without them.

TestA Header:

#import <Cocoa/Cocoa.h>
@interface TestA : NSObject {
    NSString *testString;
}
@end

TestA Implementation is empty:

#import "TestA.h"
@implementation TestA  
@end

TestB Header:

#import <Cocoa/Cocoa.h>
#import "TestA.h"
@interface TestB : TestA {
}
@property (nonatomic, retain) NSString *testProp;
@end

TestB Implementation (Error – ‘testString’ is undeclared)

#import "TestB.h"
@implementation TestB
@synthesize testProp;
- (void)testing{
    NSLog(@"test ivar is %@", testString);
}
@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-14T20:24:55+00:00Added an answer on May 14, 2026 at 8:24 pm

    I think this is the bug of GCC 4.2.1.
    I made the file foo.m with the content

    #import <Foundation/Foundation.h>
    @interface TestA : NSObject {
        NSString *testString;
    }
    @end
    
    @implementation TestA  
    @end
    
    @interface TestB : TestA {
    }
    @property (retain) NSString *testProp;
    @end
    
    @implementation TestB
    @synthesize testProp;
    - (void)testing{
    NSLog(@"test ivar is %@", testString);
    }
    @end
    

    Note that it’s OK in the 64 bit mode to omit the instance variable.
    My GCC 4.2.1 on OS X 10.6.3 gave me an error:

    $ gcc -arch x86_64 -c foo.m
    aho.m: In function ‘-[TestB testing]’:
    aho.m:19: error: ‘testString’ undeclared (first use in this function)
    aho.m:19: error: (Each undeclared identifier is reported only once
    aho.m:19: error: for each function it appears in.)
    

    This compiled without problem by changing

    NSLog(@"test ivar is %@", testString);
    

    to

    NSLog(@"test ivar is %@", self->testString);
    

    Clang compiled it without any problem.

    ( In the 32 bit mode, I got

    $ gcc -arch i386 -c foo.m
    aho.m:17: error: synthesized property ‘testProp’ must either be named 
    the same as a compatible ivar or must explicitly name an ivar
    aho.m: In function ‘-[TestB testing]’:
    aho.m:19: error: ‘testString’ undeclared (first use in this function)
    aho.m:19: error: (Each undeclared identifier is reported only once
    aho.m:19: error: for each function it appears in.)
    

    which is a perfectly expected behavior, as Manjunath wrote.)

    However I think it’s generally a rather bad idea to access an instance variable of the superclass: when you implement the methods the superclass, you cannot assume anything about the instance variable because it might be tweaked in a worst manner possible by the subclass. You at least need to write down what kind of operation on the instance variable is permitted or not… Remember you might need to maintain your code for years! I would prefer keeping programming contracts between various parts of the code at the level of methods and properties.

    Finally you should change

    @property NSString *testProp;
    

    to

    @property (copy) NSString *testProp;
    

    or at least to

    @property (retain) NSString *testProp;
    

    if you’re not using GC on OS X. Otherwise EXP_BAD_ACCESS will await you!

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

Sidebar

Related Questions

Update: It is solved .. For some reason, the data is not getting inserted
UPDATE Suddenly - without changing anything - it works again ON SOME PAGES, on
UPDATE: I now have a solution I'm much happier with that, whilst not solving
Update: This only seems to be a problem at some computers. The normal, intuitive
[ Update : Format specifiers are not the same thing as format strings; a
[update: I am using MySQL 4.1.25 ] I think this must be a simple
UPDATE: Sorry, forgot to include some of the code (face-palm). I included it in
Update : answer on how to draw one using Superformula is at the end
UPDATE: Based on Slace's response I am now using the latest Official release witch
update - if I remove the runat=server this does not happen...then how do I

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.