苹果变了valueForKey:
iOS 10 中的方法行为。
之前打电话valueForKey:
有论点nil
导致调用valueForUndefinedKey:
with nil
,如果不重写此方法,则会失败。但现在如果没有这个调用,它会立即失败。
GAITrackerModel
已覆盖valueForUndefinedKey:
,无论输入参数如何,都会返回 nil。
我可以提供方法调配,恢复以前的行为作为临时解决方案(Google 应该修复这个问题,并且此代码尚未准备好用于生产,但在那之前):
#import <objc/runtime.h>
void SwizzleInstanceMethod(Class classToSwizzle, SEL origSEL, Class myClass, SEL newSEL) {
Method methodToSwizzle = class_getInstanceMethod(classToSwizzle, origSEL);
Method myMethod = class_getInstanceMethod(myClass, newSEL);
class_replaceMethod(classToSwizzle, newSEL, method_getImplementation(methodToSwizzle), method_getTypeEncoding(methodToSwizzle));
class_replaceMethod(classToSwizzle, origSEL, method_getImplementation(myMethod), method_getTypeEncoding(myMethod));
}
@interface FixGoogleSDKiOS10 : NSObject
@end
@implementation FixGoogleSDKiOS10
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SwizzleInstanceMethod([NSObject class], @selector(valueForKey:), [self class], @selector(yb_valueForKey:));
});
}
- (nullable id)yb_valueForKey:(NSString *)key {
if (!key) {
return [self valueForUndefinedKey:key];
}
return [self yb_valueForKey:key];
}
@end