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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:00:40+00:00 2026-06-11T23:00:40+00:00

I declare a class extension interface adding vars to it. Is it possible to

  • 0

I declare a class extension interface adding vars to it. Is it possible to access those vars in a category of that class?

  • 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-11T23:00:42+00:00Added an answer on June 11, 2026 at 11:00 pm

    Sure – any variable is accessible through the runtime, even if it isn’t visible in the @interface:

    SomeClass.h

    @interface SomeClass : NSObject {
        int integerIvar;
    }
    
    // methods
    
    @end
    

    SomeClass.m

    @interface SomeClass() {
       id idVar;
    }
    
    @end
    
    @implementation SomeClass
    
    // methods
    
    @end
    

    SomeClass+Category.m

    @implementation SomeClass(Category)
    
    -(void) doSomething {
        // notice that we use KVC here, instead of trying to get the ivar ourselves. 
        // This has the advantage of auto-boxing the result, at the cost of some performance.
        // If you'd like to be able to use regex for the query, you should check out this answer:
        // http://stackoverflow.com/a/12047015/427309  
        static NSString *varName = @"idVar"; // change this to the name of the variable you need
    
        id theIvar = [self valueForKey:varName]; 
    
        // if you want to set the ivar, then do this:
        [self setValue:theIvar forKey:varName];
    }
    
    @end
    

    You can also use KVC to get iVars of classes in UIKit or similar, while being easier to use than pure runtime-hacking.

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

Sidebar

Related Questions

Possible Duplicate: Difference between Category and Class Extension? I am often told that when
I want my PHP extension to declare a class equivalent to the following PHP:
I have two questions. I know it is possible to declare class objects in
How would I declare a class that has a method/message that takes a selector,
I am trying to forward declare a class that is derived from a template
Is it possible to forward-declare a nested class, then use it as the type
I've noticed that a lot of Objective-C examples will forward declare classes with @class,
I have an Objective-c class MyClass. In MyClass.m I have a class extension that
OK, I know how to create a class extension, using something like that: on
A class can be extended in Objective C using a category such as: @interface

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.