2

i have been looking around and haven't quite found anything on doing this and am not sure if its even possible, but this is what i am trying to achieve. please note, i am giving a very very simplified example of what i am trying do. also, and resemblance of code posted is all hypothetical and is not meant to correct code syntax. it is only meant to help further illustrate the end result desired by referencing similar type functionality. so please understand the concept before offering alternative methods...

with that said, as the title says i am trying to see if i can access an object, lets just say a uilabel via its string name or a variable without using if statements. so for example, lets say we have 3 uilabes

UILabel *label1;
UILabel *label2;
UILabel *label3;

from there we do all the appropriate synthesizing and what not. now at some point i want to reference a label based on lets say and int.

int intNumber = 2;

here is the theoretical part. i would like to now set the text of a label that has the matching int value in its name.

label(%i, intNumber).text = [NSString stringWithFormat:@"This is Label %i", intValue];

the result would then be...

label2.text = @"This is Label 2";

if the int value was 1, then label1 would be changed. or if it was 3, then label3 would be changed, etc...

or if the same process could be done using the string name of the label.

int intValue = 1;

NSString *labelName;

then at some point

labelName = [NSString stringWithFormat:@"label%i",intValue];

now somehow set the text of label1 by referencing "labelName" to call it. if anyone could offer a suggestion or comment to wether something like this can even be done it would be greatly appreciated. i know you can reference classes and selectors by string name, but i'm not sure about objects? i don't know if "valueForKey:" is the way i should be looking to do this?

sorry for the long winded post, but i just wanted to make sure i stated what i wanted to do.

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
DoS
  • 1,454
  • 13
  • 18
  • sorry guys, iv actually figured it out. since i am a noob on stack overflow i can't answer my own question yet. once i am able to i will be post the answer with example code. thanks again... – DoS Apr 21 '12 at 18:27
  • possible duplicate of [Objective C Equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables), http://stackoverflow.com/questions/2231783/create-multiple-variables-based-on-an-int-count/, http://stackoverflow.com/questions/7940809/syntax-help-variable-as-object-name, – jscs Apr 21 '12 at 18:35
  • yes, it's similar but obviously not the correct answer. – DoS Apr 23 '12 at 22:35

3 Answers3

7

sorry guys & gals, i wound up figuring this out. the "setValue:forKeyPath:" turned out to be what i needed. so for an example...

example.h

@property (strong, nonatomic) UILabel *label1;
@property (strong, nonatomic) UILabel *label2;
@property (strong, nonatomic) UILabel *label3;

example.m

@synthesize label1, label2, label3;

- (void)someMethod:(int)labelVarInt {

[self setValue:[NSString stringWithFormat:@"This is Label %i", labelVarInt] forKeyPath:[NSString stringWithFormat:@"label%i.text", labelVarInt]];

}

hope this helps anyone else who is needing to do anything similar.

DoS
  • 1,454
  • 13
  • 18
  • hehe :) that is what KVC is Key Value Coding great you got your answer! – Juan Alberto López Cavallotti Apr 24 '12 at 02:33
  • yes, i know what kvc & kvo are. i just wasn't sure the best approach to take. if you noticed i even mention the "valueForKey:" method in my question. efficiency is key, and one line always trumps 5-10. many people have asked similar questions, but no one has posted an answer this simplistic. – DoS Apr 24 '12 at 14:52
2

If the case is simply indexed strings, you should probably use an array, otherwise you are looking for introspection (take a look at this question).

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
2

I think you need to store the labels in an NSArray, then you could access the labels by index.

Another approach is getting the labels using KVC.