C++ instance variable (or similar behavior) in Objective-C category .mm

Discussion in 'iOS Programming' started by moonman239, Jun 12, 2015.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    So, my goal is to have a variable whose value is unique to each instance of an Objective-C object. I want this variable to be declared in the .m file. I'm not allowed to have an instance variable in my Obj-C category's .m file. Can't even set a value for a key that doesn't already exist. My solution is to create a C++ class that I can use to store an instance variable. Since the objective is to make the data in the variable private, I've turned the category's .m file into a .mm file and created the class in there.

    Now, I have to wonder: How do I make a C++ variable whose value I can pull from within my category's Objective-C methods? On top of that, how would I pull that value?

    Thanks, guys!
     
  2. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
    I'm a bit lost here. Are you saying ObjC won't allow you to have an instance variable or are you saying the project specs won't allow it?
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    ObjC.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    There are no instance variables any more. Use an @Property.

    Then again there's the runtime function that allows you to add ivars to classes.
     
  5. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #5
    "So, my goal is to have a variable whose value is unique to each instance of an Objective-C object. I want this variable to be declared in the .m file."

    You want a variable declared in the ObjC object which is private to each instance of the object? Or do you want a variable which can be accessed externally but whose value won't change?

    In C++ the class variable is the same across all uses of the object.

    I don't really understand what you are trying to accomplish, can you give an example?
    Why do you want the variable to be declared in the .m file verses the header?
    One way to "pull" a private variable in ObjC is to create a "method" which can be called to return the variable.
     
  6. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #6
    I could use properties, but I don't like exposing data to classes that don't need it.
     
  7. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #7
    Moonman:

    It seems either one or two things are going on here, you want to use Objective C and are new to it. Or maybe you want to expose something you don't like about Object C?

    Object C is very robust, well use and will accomplish what you originally stated and can do it in multiple ways.
     

Share This Page