incrementing variable names in xcode? [duplicate] - objective-c

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.

Related

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

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

Searching an object (by an attribute) in a NSMutableArray [duplicate]

This question already has answers here:
Finding a particular instance in an array by a property
(5 answers)
Closed 8 years ago.
I need to get a better way to find an object in a NSMutableArray.
At the moment i do it this way:
for(classOfTheObject *thisItem in arrayOfObjects){
if(thisItem.foreign_key == serchThisObject.foreign_key){
// found it
}
}
but this is a very bad way i think. Is it possible to get the object without a for loop?
In an array it will always require some type of loop/enumaration to actually find it. If foreign_key is the only search/identification criteria that you use then you may consider using an NSDictionary with the value of foreign_key as key.
If I were you,I would use NSPredicate :
NSPredicate *applePred = [NSPredicate predicateWithFormat:
#"employer.name == 'Apple'"];
NSArray *appleEmployees = [people filteredArrayUsingPredicate:applePred];

Objective-C: block enumeration for array in REVERSE order? [duplicate]

This question already has answers here:
How can I reverse a NSArray in Objective-C?
(18 answers)
Closed 9 years ago.
There is the enumerateWithBlock method of NSArray, but I need to enumerate in a reverse order, is there a block for doing that, or do I have to use for loop instead, not even the fast loop, but the traditional for (int i = array.count - 1 ; i >= 0 ; i--) one?
Thanks!
You can use enumerateObjectsWithOptions:usingBlock: passing NSEnumerationReverse as the options parameter.
You can use reverseObjectEnumerator
for (id someObject in [myArray reverseObjectEnumerator])
{
// print some info
NSLog([someObject description]);
}
Hope this helps!

How to declare a variable named after the value of another variable in Objective C [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Syntax help - Variable as object name
I have a very basic beginner's question in Objective C:
I'd like to declare a variable that is named after the content/value/literal of another variable. How can I do that?
I.e.
NSString *s = [NSString stringWithFormat:#"variableName"];
// now create a second variable that is named after the literal in s
int *s = 42; // This is what doesn't work. The integer-type variable should be named variableName
Does anyone know how to do this?
Thanks for the answers so far. The reason why I am asking this questions is the following:
I have an array containing values I load from an xml file structured as follows:
<string>name</string><integer>23</integer><real>3.232</real><real>4.556</real> ... (44 times another real number)<string>nextName</string>...(and so on).
The file contains the names for MKPolygons, the number of points for each polygon and the latitude and logitude values for each point of the polygon. I managed to load the file and have its content in an array. Now I want to create MKPolygons from this array which are named as the strings in the array.
What you are looking for is a hash table. You can use NSDictionary or NSMutableDictionary or NSHashTable.
Here is an example:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:anObj forKey:#"foo"];
[dict objectForKey:#"foo"];
[dict removeObjectForKey:#"foo"];
[dict release];
This is not possible in Objective-C, nor in most mainstream languages (save for PHP). What are you trying to accomplish with this?
I don't think you can do it. However, you can create an NSDictionary and create a key called variableName and a value called 42. (an NSNumber). Then whenever you want to retrieve this value, you can just do valueForKey.

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:.