0

How I can integrate a count var (i = 1,2,3,4,...) into a object-name?

My code doesn't work.

for (int i=1; (i<=4); i++) {
    self.cmdFertigOutlet(i).layer.cornerRadius = self.cmdAbbrechenOutlet(i).layer.cornerRadius = self.lblYear(i).layer.cornerRadius = self.lblMonth(i).layer.cornerRadius = self.lblDay(i).layer.cornerRadius = self.lblHour(i).layer.cornerRadius = self.lblMinute(i).layer.cornerRadius = self.lblSecond(i).layer.cornerRadius = self.txtBeschreibung(i).layer.cornerRadius = 5;
}

.h file

@property (weak, nonatomic) IBOutlet UIButton *cmdAbbrechenOutlet1;
@property (weak, nonatomic) IBOutlet UIButton *cmdFertigOutlet1;
@property (weak, nonatomic) IBOutlet UITextField *txtBeschreibung1;
@property (weak, nonatomic) IBOutlet UILabel *lblYear1;
@property (weak, nonatomic) IBOutlet UILabel *lblMonth1;
@property (weak, nonatomic) IBOutlet UILabel *lblDay1;
@property (weak, nonatomic) IBOutlet UILabel *lblHour1;
@property (weak, nonatomic) IBOutlet UILabel *lblMinute1;
@property (weak, nonatomic) IBOutlet UILabel *lblSecond1;

@property (weak, nonatomic) IBOutlet UIButton *cmdAbbrechenOutlet2;
@property (weak, nonatomic) IBOutlet UIButton *cmdFertigOutlet2;
@property (weak, nonatomic) IBOutlet UITextField *txtBeschreibung2;
@property (weak, nonatomic) IBOutlet UILabel *lblYear2;
@property (weak, nonatomic) IBOutlet UILabel *lblMonth2;
@property (weak, nonatomic) IBOutlet UILabel *lblDay2;
@property (weak, nonatomic) IBOutlet UILabel *lblHour2;
@property (weak, nonatomic) IBOutlet UILabel *lblMinute2;
@property (weak, nonatomic) IBOutlet UILabel *lblSecond2;

and so on... 3,4

.m file instead of use this (works), i will use the for-statement. i don't understand, how to put my object-names into an array and then use it with the for-statement.

self.cmdFertigOutlet1.layer.cornerRadius =
self.cmdAbbrechenOutlet1.layer.cornerRadius = 
self.lblYear1.layer.cornerRadius = 
self.lblMonth1.layer.cornerRadius = 
self.lblDay1.layer.cornerRadius = 
self.lblHour1.layer.cornerRadius = 
self.lblMinute1.layer.cornerRadius =
self.lblSecond1.layer.cornerRadius = 
self.txtBeschreibung1.layer.cornerRadius = 5;

self.cmdFertigOutlet1.layer.masksToBounds =
self.cmdAbbrechenOutlet1.layer.masksToBounds = 
self.lblYear1.layer.masksToBounds = 
self.lblMonth1.layer.masksToBounds = 
self.lblDay1.layer.masksToBounds = 
self.lblHour1.layer.masksToBounds = 
self.lblMinute1.layer.masksToBounds = 
self.lblSecond1.layer.masksToBounds = 
self.txtBeschreibung1.layer.masksToBounds = YES;

self.cmdFertigOutlet2.layer.cornerRadius =
self.cmdAbbrechenOutlet2.layer.cornerRadius = 
self.lblYear2.layer.cornerRadius = 
self.lblMonth2.layer.cornerRadius = 
self.lblDay2.layer.cornerRadius = 
self.lblHour2.layer.cornerRadius = 
self.lblMinute2.layer.cornerRadius =
self.lblSecond2.layer.cornerRadius = 
self.txtBeschreibung2.layer.cornerRadius = 5;

self.cmdFertigOutlet2.layer.masksToBounds =
self.cmdAbbrechenOutlet2.layer.masksToBounds = 
self.lblYear2.layer.masksToBounds = 
self.lblMonth2.layer.masksToBounds = 
self.lblDay2.layer.masksToBounds = 
self.lblHour2.layer.masksToBounds = 
self.lblMinute2.layer.masksToBounds = 
self.lblSecond2.layer.masksToBounds = 
self.txtBeschreibung2.layer.masksToBounds = YES;

The object names are like:

cmdFertigOutlet1, cmdFertigOutlet2, cmdFertigOutlet3, and so on.

cmdAbbrechenOutlet1, cmdAbbrechenOutlet2, cmdAbbrechenOutlet3, and so on

Community
  • 1
  • 1
Daniela
  • 15
  • 4
  • hi and yes. cmdFertigOutlet1, cmdFertigOutlet2, cmdFertigOutlet3, and so on. cmdAbbrechenOutlet1, cmdAbbrechenOutlet2, cmdAbbrechenOutlet3, and so on..... – Daniela Jan 24 '15 at 07:41
  • See also [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), [Syntax help: variable as object name](http://stackoverflow.com/q/7940809) [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs Jan 24 '15 at 20:01

2 Answers2

2

You can't - the variables' names are compile-time properties and don't exist at runtime.

Instead, use arrays with four elements:

for (int i = 0; i < 4; i++) {
    self.cmdFertigOutlet[i].layer.cornerRadius = ...
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

I'm going to give you a simple example using NSTextField objects (to keep it simple). I think you'll understand how to carry this forward in your application.

From your syntax, I also assume you're programming in Swift. To demonstrate a technique:

In my AppDelegate class I declare IBOutlets to 4 text boxes and connect them in IB:

@IBOutlet var testTextBox0:NSTextField!
@IBOutlet var testTextBox1:NSTextField!
@IBOutlet var testTextBox2:NSTextField!
@IBOutlet var testTextBox3:NSTextField!

I also declare a mutable array:

var testTextBoxes:NSMutableArray = NSMutableArray()

Then in the applicationDidFinishLaunching function I say:

testTextBoxes.addObject(testTextBox0)
testTextBoxes.addObject(testTextBox1)
testTextBoxes.addObject(testTextBox2)
testTextBoxes.addObject(testTextBox3)

var localTextField:NSTextField = testTextBox0

for var i:Int = 0; i < 4; i++
{
    localTextField = testTextBoxes.objectAtIndex(i) as NSTextField
    localTextField.stringValue = "text field \(i)"
}

and all the text box string values get set properly.

This illustrates a technique of loading your objects into an array (or arrays for several different object types) and accessing them sequentially in a for loop.

Here's another example in Objective C using button objects this time:

transformButtons = [[NSMutableArray alloc] initWithCapacity:10]; // transformButtons is an instance variable
[transformButtons addObject:button0];
[transformButtons addObject:button1];
[transformButtons addObject:button2];
[transformButtons addObject:button3];
[transformButtons addObject:button4];
[transformButtons addObject:button5];
[transformButtons addObject:button6];
[transformButtons addObject:button7];
[transformButtons addObject:button8];
[transformButtons addObject:button9];

Then later in the code:

NSButton *localButton;

for(int i = 0; i < 10; i++)
{
    localButton = [transformButtons objectAtIndex:i];
    [localButton setEnabled:YES];
    [localButton setHidden:NO];
    [localButton setTitle:buttonNames[i]];
}

Of course in the .h file this stuff was declared as:

NSMutableArray *transformButtons;
IBOutlet NSButton *button0;
IBOutlet NSButton *button1;
IBOutlet NSButton *button2;
IBOutlet NSButton *button3;
IBOutlet NSButton *button4;
IBOutlet NSButton *button5;
IBOutlet NSButton *button6;
IBOutlet NSButton *button7;
IBOutlet NSButton *button8;
IBOutlet NSButton *button9;

Hope this helps.

EDIT2:

In the .h file, if you want to access these variables from another class, why not:

@public

IBOutlet UIButton *cmdAbbrechenOutlet1;
IBOutlet UIButton *cmdFertigOutlet1;
IBOutlet UITextField *txtBeschreibung1;
IBOutlet UILabel *lblYear1;
IBOutlet UILabel *lblMonth1;
IBOutlet UILabel *lblDay1;
IBOutlet UILabel *lblHour1;
IBOutlet UILabel *lblMinute1;
IBOutlet UILabel *lblSecond1;

IBOutlet UIButton *cmdAbbrechenOutlet2;
IBOutlet UIButton *cmdFertigOutlet2;
IBOutlet UITextField *txtBeschreibung2;
IBOutlet UILabel *lblYear2;
IBOutlet UILabel *lblMonth2;
IBOutlet UILabel *lblDay2;
IBOutlet UILabel *lblHour2;
IBOutlet UILabel *lblMinute2;
IBOutlet UILabel *lblSecond2;

// And so on.....

@private

// Your private instance variables 

In your .m file cmdAbbrechenOutlet1 could simply be accessed as cmdAbbrechenOutlet1. In another class where yourClass is visible, it could be accessed as yourClass->cmdAbbrechenOutlet1.

jscs
  • 63,694
  • 13
  • 151
  • 195
jwlaughton
  • 905
  • 1
  • 6
  • 11
  • hi. thanks for your answer. no, my program is written in objective-c not in swift. can you write it in obj-c pls..? – Daniela Jan 24 '15 at 09:05
  • hi and thanks, but doesn't work with labels and textfields. for buttons its ok. – Daniela Jan 24 '15 at 11:02
  • Hmm... should work with labels and textfields can you post some code that doesn't work for you? I'm happy to help. – jwlaughton Jan 24 '15 at 11:18
  • If you post some code that doesn't work for you, I'll paste it into one of my apps and figure out the problem. – jwlaughton Jan 24 '15 at 11:20
  • i've updated my question... see above. – Daniela Jan 24 '15 at 12:06
  • hi. i've added details in my question... see above. – Daniela Jan 24 '15 at 12:12
  • You actually need to put these into 9 arrays. e.g. cmdAbbrechenOutlet1, cmdAbbrechenOutlet2, cmdAbbrechenOutlet3 and cmdAbbrechenOutlet4 need to be in one array. cmdFertigOutlet1, cmdFertigOutlet2, cmdFertigOutlet3, cmdFertigOutlet4 need to be in a second array... and so on. – jwlaughton Jan 24 '15 at 16:01
  • BTW, I guess it's a programmer's choice about which way to go, but why all the synthesizing? You could make these variables accessible to other classes simply by declaring them after an "@public" statement in your .h file. In their home class you could drop all the self. stuff and in other classes (where yourClass is visible) you could access them as (e.g.) yourClass->cmdFertigOutlet1. Variables you want to keep private to your class should be declared after an "@private" statement. Note in my examples there is no synthesizing going on. – jwlaughton Jan 24 '15 at 16:15
  • The code in the question is clearly not Swift. Notice that all the properties are pointer types. – jscs Jan 24 '15 at 20:03