[IOS 开发] 反射

首先我们要知道,这使用的是运行时的反射机制

所以我们需要头文件 runtime.h

+(BOOL)checkoutOneTabByName:(NSString*)oneName
{
    //openDB
    FMDatabase *db = [DatabaseOperation openDataBase];
    
    NSArray *array = [DatabaseOperation queryTable:oneName QueryString:[NSString stringWithFormat:@"select * from %@",oneName] DB:db];
    
    for (id oneData in array) {
        
        unsigned int propertyCount =0;
        
        objc_property_t *FPAddress =class_copyPropertyList([oneData class], &propertyCount);
        
        for (int i =0; i<propertyCount; i++) {
            
            objc_property_t *aProp =FPAddress+i;
            
            NSString *aName =[NSString stringWithUTF8String:property_getName(*aProp)];
            
            NSObject *aValue =[oneData valueForKey:aName];
            
            NSLog(@"%@,,,%@",aName,aValue);
        }
        NSLog(@"( * ^ __ * ^ ) %@ part print success",[oneData class]);
    }
    
    //关闭数据库
    [DatabaseOperation closeDataBase:db];
    
    return YES;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。