Objective C Equivalent of PHP's “Variable Variables”
This question already has an answer here:
Using a string as name to set property values
- Core Data Transformable NSArray of IDs
- NSAttributedString ignores Autoshrink and numberOfLines for UILabel (iOS 6)
- MKPinAnnotationView color is not working
- swift_dynamicCastClassUnconditional issues in swift
- Comparing two NSDates and ignoring the time component
- What header should I include for memcpy and realloc?
2 Solutions Collect From Internet About “Objective C Equivalent of PHP's “Variable Variables””
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
- 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
- Swift: Reading a plist from MainBundle and writing directly to Documents fails
- How can I figure out what UITableViewCell I'm clicking on during a search?
- Phonegap – Save image from url into device photo gallery
- How to position view below green bar during phone call?
- XCTest not registering new methods as tests
- How to permanently fix Xcode 5 SpringBoard failed to launch application with error: -3
- Why no Infinite loop in didSet?
- How to conform to an objective c protocol in an existing .h file in Swift?
- How does rand() work? Does it have certain tendencies? Is there something better to use?
- UIScrollView frame is a different size on iOS 8 vs iOS 7
- UIKeyboard avoidance and Auto Layout
- MPMoviePlayerController rotating in full screen while the parent View Controller only supports portrait orientation
- Swift operators and nil
- CALayer vs CGContext, which is a better design approach?
- Duplicate symbols when integrating Firebase and Google Sign In SDKs manually without Cocoapods