Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C - iVar Scoped Method Variables?
    primarykey
    data
    text
    <p>I was messing around in Objective-C earlier, and I ran into a quite common situation:</p> <p>I had a class, which was not a singleton, that needed a variable shared between method calls, like <code>static</code>, but each instance needed it's own variable. However, this variable only needed to be used in one particular method, we'll call it <code>-foo</code>.</p> <p>What I'd love to do, is have a macro, let's call it <code>ivar</code>, which lets me do the following:</p> <pre><code>@implementation MyClass -(foo) { ivar int someVal = 10; // default value, ivar scoped variable. } -(bar) { someVal = 5; // error, outside of `foo`'s scope. } @end </code></pre> <p>How the variable is defined does not matter to me (either a macro like <code>OBJC_IVAR(Type, Name, Default)</code> or <code>ivar someType someName = value</code>), as long as it meets the following requirements:</p> <ul> <li>Has thread safety</li> <li>Can have variable of same name (but different value) in another method</li> <li>Type-less (doesn't matter what type the variable is)</li> <li>Default Value support</li> <li>Variable can be declared in one line (I shouldn't have to write 15 lines of code just to put a variable in my code)</li> </ul> <p>I am currently working on an Objective-C++ implementation myself, I was just wondering if anyone else had any thoughts (or existing tools) on how to do this.</p> <p>Obviously, this doesn't have to be done with a true iVar. More likely, this should be done with associated objects at run-time, which also manages deallocation for us.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload