现在的位置: 首页 > 综合 > 正文

Objective-c 反射

2017年10月31日 ⁄ 综合 ⁄ 共 4012字 ⁄ 字号 评论关闭

 

 Class test = NSClassFromString(@"TestBean");
        id obj = [[test alloc]init];
//        Class cls = object_getClass(obj);
       
//        Class test = class_getSuperclass(test1);
       
//        id obj =[[test alloc]init];
        unsigned int numberofIvars = 0;
        Ivar* ivars = class_copyIvarList(test, &numberofIvars);
        for(const Ivar* p = ivars; p< ivars+numberofIvars;p++){
            Ivar const ivar = *p ;
           const char* keyType  = ivar_getTypeEncoding(ivar);
            NSString* keyTypeValue  = [[NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            NSString* key = [NSString stringWithUTF8String:ivar_getName(ivar)];
//            NSLog(@"cTypeByte = %@", keyTypeValue);
//            if([keyTypeValue isEqual:cTypeArray]){
////                NSNumber* val = [NSNumber numberWithLongLong:10000l];
//                NSLog(@"add value ");
////                [obj setValue:@"123456" forKey:key];
//            }           
            NSLog(@"name=%@  value=%@  type=%@ ", key,[obj valueForKey:key],keyTypeValue);
   
//              NSString* str = @"testing";
//             NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
//       
//        int8_t cr[7];
//        [data getBytes:cr];
//        for(int i=0; i<7; i++){
//            NSLog(@"%d",cr[i]);
//        }
//        [obj setValue:@"testtest" forKey:@"serverBeanPath"];
//        NSLog(@"value=======%@", [obj valueForKey:@"serverBeanPath"]);
       
//            
//        int8_t len = [str lengthOfBytesUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8)];
//        
       
//        int test = 15;
//        NSData* data = [NSData dataWithBytes:&test length:sizeof(test)];
//        int8_t temp[sizeof(test)];
//        [data getBytes:temp length:sizeof(test)];
//       
//        int temp1;
//        [data getBytes:&temp length:sizeof(temp1)];
//       
//        NSLog(@"%d", temp1);

//        NSMutableArray* array = [[NSMutableArray alloc]initWithCapacity:10]; 
//        [array addObject:[NSNumber numberWithInt:2]];
//       
//        Vector* vt = [[Vector alloc]init];
//        [vt setArray:array];
//       
//        NSLog(@"%d", [vt size]);
//   
        }
       
       
       
       
       
//        u_int count;
//        objc_property_t* properties = class_copyPropertyList(test, &count);
//        free(properties);
//       
//        for(int i=0; i< count; i++){
////            objc_property_t property = properties[i];
////            char * cr = property_getName(property);
////           
////            fprintf(stdout, "%s \n",cr);
////           
////            NSString* fieldName;
////            [fieldName string]
////            SEL sel = NSSelectorFromString(fieldName);
////            Method method = class_getInstanceMethod(test, sel);
//           
////            fprintf(stdout,"%s \n",property_copyAttributeValue(property,));
//           
//           
////            fprintf(stdout, "%s \n",sel_getName(method_getName(method)));
//           
//        }
//       
////        u_int methodCount;
////       Method* methods = class_copyMethodList(test, &methodCount);
////        for(int i=0; i<methodCount; i++){
////            Method method = methods[i];
////           
////            char* dst;
////            method_getReturnType(method, dst, 8);
////            fprintf(stdout, "%s | type = %s \n",sel_getName( method_getName(method)),dst);
////        }
////        AmfMessage* msg = [[AmfMessage alloc]init];
////        Class myClass = [msg class];
////        NSObject* object =[[myClass alloc] init];
//       
//        // insert code here...
////        NSLog(@"Hello, World!");

 

       NSObject类所支持的一些基本方法。

    -(BOOL) isKindOf:class-object(判断对象是否是class-object或其子类的成员)

    -(BOOL) isMenberOfClass:class-object(判断对象是否是class-object的成员)

    -(BOOL) respondsToSelector:selector(判断对象是否能够响应selector所指定的方法)

    +(BOOL) instancesRespondToSelector:selector(判断指定的类实例是否能响应selector所指定的方法)

    +(BOOL) isSubclassOfClass:class-object(判断对象是否是指定类的子类)

    -(id) performSelector:selector(应用selector指定的方法)

    -(id) performSelector:selector withObject:object(应用selector指定的方法,传递参数object)

    -(id) performSelector:selector withObject:object1 withObject:object2(应用selector指定的方法,传递参数object1和object2)


 

【上篇】
【下篇】

抱歉!评论已关闭.