//// WJRuntime.m// RuntimeSkip//// Created by tqh on 15/9/8.// Copyright (c) 2015年 tqh. All rights reserved.//#import "WJRuntime.h"#import <objc/runtime.h>@implementation WJRuntime//runtime跳轉(zhuǎn)+ (void)runtimePush:(NSString *)vcName dic:(NSDictionary *)dic nav:(UINavigationController *)nav { //類名(對象名) NSString *class = vcName; const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding]; Class newClass = objc_getClass(className); if (!newClass) { //創(chuàng)建一個類 Class superClass = [NSObject class]; newClass = objc_allocateClassPair(superClass, className, 0); //注冊你創(chuàng)建的這個類 objc_registerClassPair(newClass); } // 創(chuàng)建對象(寫到這里已經(jīng)可以進(jìn)行隨機(jī)頁面跳轉(zhuǎn)了) id instance = [[newClass alloc] init]; //下面是傳值-------------- [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { if ([self checkIsExistPRopertyWithInstance:instance verifyPropertyName:key]) { //kvc給屬性賦值 [instance setValue:obj forKey:key]; }else { NSLog(@"不包含key=%@的屬性",key); } }]; [nav pushViewController:instance animated:YES]; }/** * 檢測對象是否存在該屬性 */+ (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName{ unsigned int outCount, i; // 獲取對象里的屬性列表 objc_property_t * properties = class_copyPropertyList([instance class], &outCount); for (i = 0; i < outCount; i++) { objc_property_t property =properties[i]; // 屬性名轉(zhuǎn)成字符串 NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; // 判斷該屬性是否存在 if ([propertyName isEqualToString:verifyPropertyName]) { free(properties); return YES; } } free(properties); return NO;}@end
|
新聞熱點(diǎn)
疑難解答