Reputation: 140
I am trying to NSLog
, objects/properties of a viewController.
I have worked on looping through subviews,superviews (basically UIElements) like in below code
@interface ViewController : UIViewController
{
NSString *string;
NSMutableArray *mutableArray ;
NSMutableDictionary *mutableDictionary;
}
@property NSString *string;
@property NSMutableArray *mutableArray ;
@property NSMutableDictionary *mutableDictionary;
@implementation ViewController
-(void) loopThrough{
for (id obj in [self.view subviews]) {
nslog(@"This would print subviews properties%@", obj)
}
}
My question is similar to the above is it possible to loop through set of Non UI elements per se NSString, NSArray and etc.,
Implementation Scenario
I have 4 network call timeout timers in viewController and when even one is succeeded with network calls, timeout timer has to be disabled. But since there are four network calls happening, I don't want to declare 4 timer global variables of viewController and invalidate each timer separately. Rather I would like to loop through and invalidate timers.
Upvotes: 1
Views: 1384
Reputation: 4789
You need to use Objective C run-time libraries for this
#import <objc/runtime.h>
- (NSSet *)propertyNames {
NSMutableSet *propNames = [NSMutableSet set];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
[propNames addObject:propertyName];
}
free(properties);
return propNames;
}
- (void)loopThrough {
for(NSString *key in [self propertyNames]) {
NSLog (@"value = %@ , property %@",[self valueForKey:key],key);
}
}
Upvotes: 6