Dil*_*ttu 1 iphone objective-c nslog objective-c-runtime ios
我正在尝试NSLog,viewController的对象/属性.
我已经开始循环子视图,超级视图(基本上是UIElements),如下面的代码
@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)
}
Run Code Online (Sandbox Code Playgroud)
}
我的问题与上面类似,可以循环遍历非UI元素集NSString,NSArray等,
Implementation Scenario
我在viewController中有4个网络调用超时计时器,当一个网络调用成功时,必须禁用超时计时器.但由于发生了四次网络调用,我不想声明viewController的4个定时器全局变量,并分别使每个定时器无效.相反,我想循环并使计时器无效.
您需要使用Objective C运行时库
#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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1164 次 |
| 最近记录: |