Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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!
 
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.

Thanks, guys!

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?
 
"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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.