Accessing variables with related names in a for loop [duplicate] - objective-c

This question already has an answer here:
Syntax help - Variable as object name [duplicate]
(1 answer)
Closed 8 years ago.
I have three NSRects in separate variables named rect1, rect2, and rect3, and I want to increment each one's origin.x by 10.
I thought a for-loop would be good for this, but I don't know how I can do it.
This is an example of how I want it to work:
for(int i=0, i<3, i++) {
rect[i].origin.x +=10
}
but this exact code gives an error
property "rect" not found on object of type "graphics"
Is there a way to code it like in my example?

Is there a way to increment a variable with for loop
Not in objective-c for sure. You cannot declare variables in 0-n format and loop through it.
If you create an array out of your NSRect variables then you can loop through the array and modify properties.
Note: NSArray can only hold Objects not primitives, you might find this handy

Related

Is it possible to make a variable dynamic? [duplicate]

This question already has an answer here:
Using a string as name to set property values
(1 answer)
Closed 6 years ago.
In PHP and in JavaScript you can dynamically use a variable by using another variable as part of its name. I do not want to use an array as part of the solution. Can this be achieved in Objective C? I'd like to do something like this:
for (int i=1; i<6; i++) {
if([appRecord.answer(i) length] != 0){
self.textView.answer(i)ViewSingle.text = appRecord.answer(i);
}
}
Short answer: No.
Long answer: Kind of.
You can use an array, store your variables in there, and index into it.
Like #1, if your objects are actually UI elements and you don't want a whole bunch of IBOutlets, then use an IBOutletCollection instead.
You can use a dictionary, store your variables as values, and look them up by key.
You can declare all your variables as #property, and then use [self valueForKey:] to look them up by name.
You can build the name of the ivar as a string, and then use something like object_getInstanceVariable() to retrieve it's value directly (this is similar to #3, except you don't have to declare it as an #property). This is excessively complicated and is usually a much bigger hammer than you'll actually need.
If you're dealing with views, you can assign each view a unique tag and then retrieve it via [superview viewWithTag:aTag]. I do not recommend using this approach.
EDIT: Note that this only works with instance variables. This does not work with global/static variables.
No. But you can give a tag to the view and use -viewWithTag:.

Random Number Generator Objective-C iOS8 [duplicate]

This question already has answers here:
Picking a Random Object in an NSArray
(8 answers)
Closed 8 years ago.
My app has a mutable array in a data model (used to pass data between views) which allows users to add the names of 'housemates' as strings to a table view controller. I want to later generate a random housemate in another view controller i.e. when a button is pressed I want the label underneath it to display the randomly selected housemate. I wondered if I could assign each housemate a number and, when a random number is generated, use an if/else statement to say something like:
if randomNumber == 0 {
self.label.text = self.housemate0.text;
}
But I'm unsure how to count all the housemates in the mutable array and then assign/tag them with a number to later be generated and how the actual code for generating the number/updating the label would work. Could anyone help me please? Many thanks.
A NSArray, or NSMutableArray is index based, so every entry has an unique index number to access it like myarray.objectAtIndex:1
And counting your array will be done by myarray.count
Is that what you expected with your question?!? :-)

Getting a list of all object properties [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get an object attributes list in Objective-C
I am trying to find a way to iterate through all properties on an object and assign the first non empty property's value to another variable. All these properties are string and i am trying to use isEqualToString:#"" to check each property manually before assigning the first non empty one to another variable. Is there a way to do this efficiently by iterating through each property instead of having if conditionals checking each property individually?
Giuseppe's answer is good but it seems you don't need to complicate yourself with that.
If you say all propertys are strings, why not use an NSArray and ,using fast enumeration, check if the string is in the array , if not just add it. That way you don't have a limit (in case you need to modify the project later).
Also this could work even with different types of variables/propertys. When you loop through the array just use isSubclassOfClass: to check type.

incrementing variable names in xcode? [duplicate]

This question already has answers here:
Objective C Equivalent of PHP's "Variable Variables" [duplicate]
(2 answers)
Closed 6 years ago.
Incrementing variable names
In xcode I want to create 100 images one with the name1,2,3,4 ect. And i can do it all except for the variable names. For these I was wandering is it possible to increment the variables programmatically or whether it must be done manually?
Instead of creating a bunch of variable names, you can use a dictionary object with the names as keys and the image object as the data.
If you really want to/have to use 100 different variables and avoid an array you could use Key Value Coding in combination with a for loop:
NSString *name = #"myVariable";
for (int i = 0; i< 100; i++)
{
// get aValue from somewhere...
NSString *key = [NSString stringWithFormat:#"%#%d", name, i];
[self setValue:aValue forKey:key];
}
This sort of thing is best achieved with an array.
Similar question here: Objective C Equivalent of PHP's "Variable Variables"
Create them in an NSMutableArray. Doesn't matter if all of them have the same object name, since they have different locations in the array. Just access them with [arrayName objectAtIndex:index]; Use a for.

Objective C Equivalent of PHP's "Variable Variables" [duplicate]

This question already has an answer here:
Using a string as name to set property values
(1 answer)
Closed 6 years ago.
In PHP and in JavaScript you can dynamically use a variable by using another variable as part of its name. I do not want to use an array as part of the solution. Can this be achieved in Objective C? I'd like to do something like this:
for (int i=1; i<6; i++) {
if([appRecord.answer(i) length] != 0){
self.textView.answer(i)ViewSingle.text = appRecord.answer(i);
}
}
Short answer: No.
Long answer: Kind of.
You can use an array, store your variables in there, and index into it.
Like #1, if your objects are actually UI elements and you don't want a whole bunch of IBOutlets, then use an IBOutletCollection instead.
You can use a dictionary, store your variables as values, and look them up by key.
You can declare all your variables as #property, and then use [self valueForKey:] to look them up by name.
You can build the name of the ivar as a string, and then use something like object_getInstanceVariable() to retrieve it's value directly (this is similar to #3, except you don't have to declare it as an #property). This is excessively complicated and is usually a much bigger hammer than you'll actually need.
If you're dealing with views, you can assign each view a unique tag and then retrieve it via [superview viewWithTag:aTag]. I do not recommend using this approach.
EDIT: Note that this only works with instance variables. This does not work with global/static variables.
No. But you can give a tag to the view and use -viewWithTag:.