Dynamically generated property / function call in a C object

Is it possible to dynamically build a function or function call? I have a set of views that I want to do in the same way. Therefore, if part of my code looks like this

self.ViewName.hidden = NO;

and I want to use a variable for the name of the view, is there a way to do this, something like

self {var} .hidden = NO;

Where 'var' is the NSString of the view name and is evaluated at runtime? I know this will not work with angle brackets, just to indicate how I am trying to build a property reference.

thank

+3
source share
3 answers

NSSelectorFromString. , _ ,

[[self performSelector:NSSelectorFromString(@"ViewName")] setHidden:NO];
+2

setValue:forKeyPath::

NSString* path = [NSString stringWithFormat:@"%@.hidden", viewName];
[self setValue:[NSNumber numberWithBool:YES] forKeyPath:path];
0

If you have several views, you must put them in an array and access each element of the array separately.

NSMutableArray * views...
[[views objectAtIndex:i] setHidden:NO];
0
source

Source: https://habr.com/ru/post/1758213/


All Articles