0

Say I have n variables

NSNumber* A = @(1);
NSNumber* B = @(2);
NSNumber* C = @(3);
NSNumber* D = @(4);
NSNumber* E = @(5);
...

I need a dictionary like

{@"A":@(1), @"B":@(2), @"C":@(3), @"D":@(4), ... }

One can imagine a more cumbersome example that would be tedious to type out

I think saw a C style function for it but I can't remember. Something like NSDictionaryForVariables()

Brian
  • 14,610
  • 7
  • 35
  • 43
adamF
  • 961
  • 2
  • 9
  • 23
  • 4
    please explain a little bit more what you need. your current example is too simplistic. just a simple for-loop would do. Do you want to iterate over the variables? In what way? Do you want to access the variable by its string name? – luk2302 Jul 15 '15 at 18:09
  • The C function you're looking for is: "NSDictionaryOfVariableBindings". – dudeman Jul 15 '15 at 18:12
  • 6
    You're very likely trying to pound a nail with a shoe. If you tell us about the nail and the boards you're trying to join, we can probably point you in the direction of a suitable hammer. – jscs Jul 15 '15 at 18:13
  • You can actually get the name of a variable (did not know that was possible) http://stackoverflow.com/questions/2484778/anyway-to-get-string-from-variable-name But you still have to write the name of the variable in the `(...)`, therefore you could just use the string in the first place... – luk2302 Jul 15 '15 at 18:15

2 Answers2

1

It's not a good approach you may find another way to solve your issue but if you want to have an idea about your requested solution here here my try

Our properties

   @interface TestyViewController ()
        @property (nonatomic) NSNumber* a;
        @property (nonatomic) NSNumber* b;
        @property (nonatomic) NSNumber* c;
        @property (nonatomic) NSNumber* d;
    @end

Set the values

- (void)viewDidLoad {
    [super viewDidLoad];

    self.a=@(1);
    self.b=@(2);
    self.c=@(3);
    self.d=@(4);
}

Get our instance variables

-(NSArray *)propertyNames{

    unsigned int propertyCount = 0;
    objc_property_t * properties = class_copyPropertyList([self class], &propertyCount);

    NSMutableArray * propertyNames = [NSMutableArray array];
    for (unsigned int i = 0; i < propertyCount; ++i) {
        objc_property_t property = properties[i];
        const char * name = property_getName(property);
        [propertyNames addObject:[NSString stringWithUTF8String:name]];
    }
    free(properties);


    return propertyNames;
  }

Create the dictionary

- (IBAction)buttonClicked:(id)sender
{
    NSMutableDictionary *dict =  [[NSMutableDictionary alloc] init];

    for (NSString* varName in [self propertyNames])
    {
        [dict setObject:[self valueForKey:varName] forKey:varName];
    }

    NSLog(@"%@",dict);

}

result

2015-07-15 20:30:56.546 TestC[879:27973] {
    a = 1;
    b = 2;
    c = 3;
    d = 4;
}
Alaeddine
  • 6,104
  • 3
  • 28
  • 45
1

The C preprocessor macro (not a function) you're looking for is NSDictionaryOfVariableBindings. However, outside of Auto Layout (and, debatably, even there), it's not really a great idea to be setting up dependencies between runtime and compile-time identifiers like that.

Depending on what you're trying to actually accomplish, Key-Value Coding might be a better solution.

rickster
  • 124,678
  • 26
  • 272
  • 326