- (void)getDeviceInfoDeleteConcifrm:(HistoricalRecord *)record :(WXModuleCallback)callback {
NSString *deviceName = nil;
if ([[record valueForKey:@"way"] isEqualToString:@"1"]) {//電腦端
deviceName = [record valueForKey:@"deviceName"] ? [NSString stringWithFormat:@"此%@",[record valueForKey:@"deviceName"]] : @"此電腦端網(wǎng)頁";
} else if ([[record valueForKey:@"way"] isEqualToString:@"2"]) {//APP
deviceName = [record valueForKey:@"deviceName"] ? [record valueForKey:@"deviceName"] : @"蘋果設(shè)備";
} else {
deviceName = [record valueForKey:@"deviceName"] ? [record valueForKey:@"deviceName"] : @"";
....
}
第一慎式、參數(shù)類型不應(yīng)該是一個類,而應(yīng)該是一個數(shù)組或者字典或者字符串趟径,看代碼情況應(yīng)該是傳的字典瘪吏。
如果在不改變參數(shù)類型的情況下,把字典當(dāng)做一個類去直接取屬性way
蜗巧,也就是直接在用調(diào)用-way
方法時能自動取字典的way
key的值就很完美了掌眠。
費話不多說,直接上代碼幕屹,已實現(xiàn)功能蓝丙,后續(xù)有問題會及時跟進(jìn)修復(fù)级遭。
直接main.m
就行
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
@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;
}