Is it possible to reference a variable with a string and an int?
int number1; int j = 1; @"number%i", j = 3; //Hope this makes sense..
The code above gives me warnings and does not work, how could this be done.
I also tried this, but it doesnt work (for quite obvious reasons):
int j = 1; NSString *refString = [NSString stringWithFormat:@"number%i", j]; refString = 3;
2 Solutions Collect From Internet About “Is it possible to reference a variable with a string and an int?”
This is an anti-pattern I call the Poor Man’s Array. The better way to do this is to use a proper collection like an array instead of a bunch of variables that are secretly related. Done right, the code with an array will usually be a lot shorter and cleaner too.
From what I can infer you are trying to set/retrieve different variables based on the value of
You could use a dictionary for this purpose:
NSMutableDictionary *numbers = [NSMutableDictionary dictionary]; int j = 1; [numbers setObject:[NSNumber numberWithInt:3] forKey:[NSNumber numberWithInt:j]];
And then to retrieve:
[[numbers objectForKey:[NSNumber numberWithInt:j]] intValue];
It’s a bit verbose, but you could simplify it by creating a small class.
- problem with commitEditingStyle -to delete cell in the table
- Automatically download sales reports from iTunes Connect
- How to clear/empty pasteboard on viewWillDisappear
- Sudden Warning on XCode 5.1
- How to enable audio scrubber in iOS Lock Screen control panel?
- Join multiple audio files into one
- Custom tab bar icon colors
- What is the difference between .dylib and .a lib in ios?
- NSDictionary With Integer Values
- How can I determine which UIControlEvents type caused a UIEvent?
- How to disable a constraint programatically?
- Add a ContainerView inside a UIViewController created from .xib
- How to convert Objective-C TOCropViewController delegate method in Swift?
- Why shoudn't I use accessor methods in init methods
- What is Container View in iOS 5 SDK?