views:

145

answers:

3

Hi All,

Two Complete newbie questions about programming in ObjC - using ideas from other languages I've used in the past. It'd be great if such concepts existed in ObjC, but all the info I've been able to gather so far is all about the simple stuff like "for" loops, etc.

Question 1

Is there a way in Objective C of evaluating a variable first before executing a line?

For example, let's say I have 5 labels for which I want to change the text. I could do something like:

Label1.text = array(1)
Label2.text = array(2)
Label3.text = array(3)
Label4.text = array(4)
Label5.text = array(5)

but what I REALLY want to do is this:

for (x=0; x<=5; x++) {
Label'x'.text = array(x)
}

Is this even possible in Objective C? If so, any idea what it's called or what the syntax is?

Question 2

Related to the above, is there any way to do something similar with objects?

example:

foo = objectA
'foo'.textcolor = red
foo = objectB
'foo'.textcolor = green

so... then there are 2 objects with different text colors -

objectA is red, objectB is green. foo is just a "placeholder" or "stand-in" object without any properties itself.

Does any of this make sense to anyone else?

LOL

Thanks in advance,

-Leevy

+3  A: 

Both are impossible.

Question 1

What you really should be doing is using an array to store the labels. And a separate array for the values.

Question 2

I can't think of a situation where this would really be necessary, in the end this could introduce some really hard to find bugs.

Reason

In most compiled programming languages names are just around for the developer. After using the compiler they get converted to an address and the name of the variable is lost.

Georg
not impossible, just impractical. This is *C*, remember. We can do anything.
Dave DeLong
Just because we can doesn't mean we should!
Sneakyness
@Sneakyness that's a different matter entirely ;) I whole heartedly agree that there's no good use-case for a feature like this. The most practical workaround is to use an array (as `gs` suggests) or to store the labels in a dictionary (so you can reference them by a key).
Dave DeLong
+2  A: 

Question 1

I would not recommend this, but if you really wanted to do it, and if your labels were exposed as properties:


int i;
NSArray *myArray = [NSArray arrayWithObjects:@"Text1", @"Text2", @"Text3", @"Text4", @"Text5", nil];

for (i = 0; i < 5; i++)
{
    SEL aSel = NSSelectorFromString([NSString stringWithFormat:@"Label%d", i + 1]);
    [[self performSelector:aSel] setText:[myArray objectAtIndex:i]];
}

NB

Not tested.

dreamlax
+1 clever way to get at the properties. You could probably also use `[self valueForKey:[NSString stringWithFormat:@"Label%d", i+1]]`.
Dave DeLong
Ooooh, I forgot about `valueForKey:`! That's a much nicer approach.
dreamlax
I just want to reiterate that this won't work if Label1..Label5 are plain old local variables. It works because we get them by sending separate messages to an object. In Objective-C, selectors (the messages that we send) are pretty dynamic, but targets (the objects to which we send the messages) aren't. Don't know if that will help clear things up or just make things more confusing.
Daniel Yankowsky
A: 

Question 1

You could maybe do something like this:

LABEL Label1;
LABEL Label2;
LABEL Label3;
LABEL Label4;
LABEL Label5;

LABEL *Labels = &Label1;

for(int i = 0; i < 5; i++)
    Labels[i].text = array(i);

However, if this ever works, it is guaranteed that it will NOT ALWAYS work, and I do not suggest using it. Whether this works or not depends on how the compiler reserves the space for the labels (something which changes from compiler to compiler and from compiler settings to compiler settings).

Question 2

The closest thing to this question that I can think of is this:

Since you want to set the text color of two objects, I presume that their behaviour is related. If objectA and objectB are two objects of the same class, you could do

TheClass *objectA = [[TheClass alloc] init];
TheClass *objectB = [[TheClass alloc] init];

TheClass *foo = objectA;

[foo setTextColor:RED];
foo = objectB;
[foo setTextColor:BLUE];

If they are NOT objects of the same class, you could have them inherit from the same superclass, and do something like this:

SubClassA *objectA = [[SubClassA alloc] init];
SubClassB *objectB = [[SubClassB alloc] init];
SuperClass *foo = objectA;

[foo setTextColor:RED];
foo = objectB;
[foo setTextColor:BLUE];

The implementation of setTextColor would be different in all 3 classes. In SuperClass it would be an empty function, while in the two SubClass classes it would be specific to the Class.

ajcaruana