iOS runtime 实际运用 解决weex传递字典给原生,而开发人员当做对象来处理的情况

- (void)getDeviceInfoDeleteConcifrm:(HistoricalRecord *)record :(WXModuleCallback)callback {
    NSString *deviceName = nil;
    if ([[record valueForKey:@"way"] isEqualToString:@"1"]) {//电脑端
        deviceName = [record valueForKey:@"deviceName"] ? [NSString stringWithFormat:@"此%@",[record valueForKey:@"deviceName"]] : @"此电脑端网页";
    } else if ([[record valueForKey:@"way"] isEqualToString:@"2"]) {//APP
        deviceName = [record valueForKey:@"deviceName"] ? [record valueForKey:@"deviceName"] : @"苹果设备";
    } else {
        deviceName = [record valueForKey:@"deviceName"] ? [record valueForKey:@"deviceName"] : @"";
....
}

第一、参数类型不应该是一个类,而应该是一个数组或者字典或者字符串,看代码情况应该是传的字典。
如果在不改变参数类型的情况下,把字典当做一个类去直接取属性way,也就是直接在用调用-way方法时能自动取字典的waykey的值就很完美了。
费话不多说,直接上代码,已实现功能,后续有问题会及时跟进修复。
直接main.m就行


#import 
#import 
#import 
@interface Person:NSObject
@property(nonatomic, copy) NSString *name;
@end

@implementation NSDictionary(NSException)
NSString *tempKey;
id dynamicGetterIMP(id self, SEL _cmd) {
    if([self isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *)self;
        if ([dict.allKeys containsObject:tempKey]) {
            return [self valueForKeyPath:tempKey];
        }
    }
    return nil;
}
void dynamicSetterIMP(id self, SEL _cmd, id value) {
    if([self isKindOfClass:[NSMutableDictionary class]]) {
        NSMutableDictionary *dict = (NSMutableDictionary *)self;
        if ([dict.allKeys containsObject:tempKey]) {
            [dict setObject:value forKey:tempKey];
        }
    }
}
+ (BOOL)resolveInstanceMethod:(SEL)aSEL
{
    
    NSString *selString = NSStringFromSelector(aSEL);
    if(![selString containsString:@":"]) {
        tempKey = nil;
        tempKey = NSStringFromSelector(aSEL);
        class_addMethod(self, aSEL, (IMP)dynamicGetterIMP, "v@:");
        return YES;
    } else if (selString.length >= 4 &&
               [selString hasPrefix:@"set"] &&
               [selString componentsSeparatedByString:@":"]) {
        
        if ([selString characterAtIndex:3] >= 'A' && [selString characterAtIndex:3] <= 'Z') {
            NSString *noMaoHaoString = [selString stringByReplacingOccurrencesOfString:@":" withString:@""];
            NSString *noSetString = [noMaoHaoString stringByReplacingOccurrencesOfString:@"set" withString:@""];
            NSString *firstString = [[NSString stringWithFormat: @"%C", [noSetString characterAtIndex:0]] lowercaseString];
            NSString *otherString =
            [noSetString stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
            tempKey = nil;
            tempKey = [firstString stringByAppendingString:otherString];
            class_addMethod(self, aSEL, (IMP)dynamicSetterIMP, "v@:@");
            return YES;
        }
        
    }
    return [super resolveInstanceMethod:aSEL];
}

@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSDictionary *dict = @{@"name":@"nameValue"};
        Person *person = dict;
        NSLog(@"----%@---", person.name);
        person.name = @"111";
        NSLog(@"----%@---", person.name);
        
        NSMutableDictionary *dictMu = @{@"name":@"nameValue"}.mutableCopy;
        Person *person2 = dictMu;
        NSLog(@"----%@---", person2.name);
        person2.name = @"111";
        NSLog(@"----%@---", person2.name);
        
    }
    return 0;
}

你可能感兴趣的:(iOS runtime 实际运用 解决weex传递字典给原生,而开发人员当做对象来处理的情况)