user233511
user233511

Reputation:

Easy Programming - an Objective C syntax question

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

Upvotes: 0

Views: 245

Answers (3)

ajcaruana
ajcaruana

Reputation: 505

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.

Upvotes: -1

dreamlax
dreamlax

Reputation: 95385

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.

Upvotes: 2

Georg Sch&#246;lly
Georg Sch&#246;lly

Reputation: 126175

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.

Upvotes: 3

Related Questions