I’d like to change the value of a parameter at runtime and curious how this works in Obj-C.
I have a loop where value of ‘n’ is 0 increasing by 1 with each loop. How does one increment the passed parameter by 1 as n moves.
UIViewSubclass *uiViewSubclass = [[UIViewSubclass alloc] initWithValue:([value integerValue])
andPlacement:kPlacement0;
next time through loop I’d like 2nd argument to read as: andPlacement:kPlacement1;
and then: andPlacement:kPlacement2; and on…
do I make kPlacement a string and stringByAppendingString:[[NSNumber numberWithInt:n] stringValue]; ?
What’s the Obj-C/Cocoa approach?
You can’t modify your source code or make up variable references at runtime. Objective-C isn’t that dynamic.
If the values of
kPlacement0throughkPlacementMaxare sequential, you may be able to use aforloop to step through them directly:(You will need to define
kPlacementIncrementandkPlacementMaxin addition to thekPlacement0, etc. constants. I’m usingMyPlacementas the name of your enumeration type that thekPlacement0etc. constants correspond to.)If they are not sequential, put them in a C array and iterate on that array:
You probably can come up with more-descriptive names than
kPlacement0etc. When you want to refer to them by number, do that; when you want to refer to them by name, give them good names.