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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:23:26+00:00 2026-05-19T16:23:26+00:00

I can’t access member variables of a sub class after sending object as super

  • 0

I can’t access member variables of a sub class after sending object as super class. Member functions are working. See this example:

super class definition

@interface SuperClass : NSObject {
 NSString *stringSuper;
 NSInteger intSuper;
}
- (void)test; // NSLog(@"test: SuperClass");

derived class definition

@interface DerivedClass : SuperClass {
 NSString *stringDerived;
 NSInteger intDerived;
}
- (void)test; // NSLog(@"test: DerivedClass");
- (void)test1; // NSLog(@"test1: DerivedClass");

create a DerivedClass object and send as SuperClass

DerivedClass *d = [[DerivedClass alloc] init];
d.stringSuper = anotherString;
d.intSuper = anotherInt;
d.stringDerived = anotherString1;
d.intDerived = anotherInt1;

[anObject sendMessage:d];

try to access members after cast to DerivedClass

- (void)sendMessage:(SuperClass *)s {
 DerivedClass *d = (DerivedClass *)s;
 NSLog(@"%@", d.stringSuper); // ok
 NSLog(@"%d", d.intSuper); // ok
 NSLog(@"%@", d.stringDerived); // EXC_BAD_ACCESS
 NSLog(@"%d", d.intDerived); // ok

 [d test]; // ok ("test DerivedClass")
 [d test1]; // ok ("test1 DerivedClass")
}

I’ve declared @properties (nonatomic, assign) for all members. The debugger shows me the correct address for stringDerived, but it is “out of scope”.

Does anybody know what is the reason for this behavior?

Thanks, rodo

  • 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-19T16:23:26+00:00Added an answer on May 19, 2026 at 4:23 pm

    Yes: you have violated rule 1 of how to do properties. Clearly anotherString1 was released before the point you read from d.stringDerived; it may even have been invalid at the point you set d.stringDerived. Equally, anotherString is obviously either a constant string or retained by something else. It would have been more obvious if you had shown your actual code.

    If you define a class with an object-type property you must arrange for instances of your class to copy or retain the value assigned to that property in order to be assured that it will still be there when you next look. If you use assign the value will probably become invalid very quickly as it is likely to be an autoreleased value. In general, if the property type conforms to the NSCopying protocol and it is mutable or has a mutable subclass (for example: NSString, NSData, NSSet, NSArray, etc) it is best to use copy so that the value doesn’t change once set; in other cases it is fine to use retain.

    Having done this you must also ensure that the memory used by the property value is correctly accounted-for when the object is deallocated. Set the value of any object-type properties to nil in your dealloc routine.

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

Sidebar

Related Questions

Can you set the internal [[Class]] property of an ECMAScript object?
Can I run this in a Windows command prompt like I can run it
Can anybody help me? What should be the datatype for this type -07:00:00 of
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can't figure out how to do this in a pretty way : I have
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
can anyone tell me if it is possible to convert a dojo charting object
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());
Can't work out a way to make an array of buttons in android. This

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.