Bra*_*rst 6 macos cocoa objective-c
ARC已启用.
我有一个类型为SEL的属性的类: @property SEL mySelector;
它是合成的: @synthesize mySelector;
然后我尝试使用KVC为它分配一个值:
SEL someSelector = @selector(doSomething:)
NSValue* someSelectorValue = [NSValue value:someSelector withObjCType:@encode(SEL)];
[target setValue:someSelectorValue forKey:@"mySelector"];
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
[<LACMyClass 0x101b04bc0> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key mySelector.
Run Code Online (Sandbox Code Playgroud)
这显然不正确 - 该类符合KVC,它只是不喜欢我传入的值.它似乎在我定义类型的属性void*而不是SEL,但这不符合我的要求.
除了用value:withObjCType:,我也试过valueWithBytes:objCType:和valueWithPointer:
任何人都可以向我解释
似乎只有默认setValue:forKey:实现的自动装箱/拆箱支持原始类型的某个子集.请参阅"键 - 值编码编程指南" 的"标量和结构支持"一章中的表1和表2 .这里的含义是,只有BOOL,char,double,float,int,long,long long,short,和它们对应的无符号的充分支持,随着structs可通过NSValue.其他类型(例如SEL和其他指针值)似乎不受支持.
考虑以下程序:
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@property (nonatomic) SEL mySelector;
@property (nonatomic) void *myVoid;
@property (nonatomic) int myInt;
@property (nonatomic,unsafe_unretained) id myObject;
@end
@implementation MyObject
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
SEL selector = @selector(description);
NSValue *selectorValue = [NSValue valueWithPointer:selector];
NSValue *voidValue = [NSValue valueWithPointer:selector];
NSValue *intValue = @1;
__unsafe_unretained id obj = (__bridge id)(const void *)selector;
MyObject *object = [[MyObject alloc] init];
// The following two calls succeed:
[object setValue:intValue forKey:@"myInt"];
[object setValue:obj forKey:@"myObject"];
// These two throw an exception:
[object setValue:voidValue forUndefinedKey:@"myVoid"];
[object setValue:selectorValue forKey:@"mySelector"];
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以设置int和id属性就好 - 甚至使用__unsafe_unretained和桥接强制转换让我们传递选择器值.但是,不支持尝试设置两种指针类型中的任何一种.
我们如何从这里开始?我们可以,例如,覆盖valueForKey:并setValueForKey:在MyObject支持拆箱要么SEL类型或拦截特定密钥.后一种方法的一个例子:
@implementation MyObject
- (id)valueForKey:(NSString *)key
{
if ([key isEqualToString:@"mySelector"]) {
return [NSValue valueWithPointer:self.mySelector];
}
return [super valueForKey:key];
}
- (void)setValue:(id)value forKey:(NSString *)key
{
if ([key isEqualToString:@"mySelector"]) {
SEL toSet;
[(NSValue *)value getValue:&toSet];
self.mySelector = toSet;
}
else {
[super setValue:value forUndefinedKey:key];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
在使用中,我们发现它按预期工作:
[object setValue:selectorValue forKey:@"mySelector"];
NSString *string = NSStringFromSelector(object.mySelector);
NSLog(@"selector string = %@", string);
Run Code Online (Sandbox Code Playgroud)
这会将"selector string = description"记录到控制台.
当然,这有可维护性问题,因为您现在必须在使用KVC设置选择器所需的每个类中实现这些方法,并且还必须与硬编码键进行比较.解决这个风险的方法之一是使用方法调配并NSObject用我们自己的方法替换替换的KVC方法的实现,它们可以处理SEL类型的装箱和拆箱.
以第一个例子为基础的以下程序源自Mike Ash的精彩"let's build KVC"文章,并且还使用了这个答案中的Swizzle()函数.请注意,我为了演示而偷工减料,并且此代码仅适用于具有适当命名的getter和setter的属性,并且不会直接检查实例变量,这与默认的KVC实现不同.SEL
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface MyObject : NSObject
@property (nonatomic) SEL mySelector;
@property (nonatomic) int myInt;
@end
@implementation MyObject
@end
@interface NSObject (ShadyCategory)
@end
@implementation NSObject (ShadyCategory)
// Implementations of shadyValueForKey: and shadySetValue:forKey: Adapted from Mike Ash's "Let's Build KVC" article
// http://www.mikeash.com/pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html
// Original MAObject implementation on github at https://github.com/mikeash/MAObject
- (id)shadyValueForKey:(NSString *)key
{
SEL getterSEL = NSSelectorFromString(key);
if ([self respondsToSelector: getterSEL]) {
NSMethodSignature *sig = [self methodSignatureForSelector: getterSEL];
char type = [sig methodReturnType][0];
IMP imp = [self methodForSelector: getterSEL];
if (type == @encode(SEL)[0]) {
return [NSValue valueWithPointer:((SEL (*)(id, SEL))imp)(self, getterSEL)];
}
}
// We will have swapped implementations here, so this call's NSObject's valueForKey: method
return [self shadyValueForKey:key];
}
- (void)shadySetValue:(id)value forKey:(NSString *)key
{
NSString *capitalizedKey = [[[key substringToIndex:1] uppercaseString] stringByAppendingString:[key substringFromIndex:1]];
NSString *setterName = [NSString stringWithFormat: @"set%@:", capitalizedKey];
SEL setterSEL = NSSelectorFromString(setterName);
if ([self respondsToSelector: setterSEL]) {
NSMethodSignature *sig = [self methodSignatureForSelector: setterSEL];
char type = [sig getArgumentTypeAtIndex: 2][0];
IMP imp = [self methodForSelector: setterSEL];
if (type == @encode(SEL)[0]) {
SEL toSet;
[(NSValue *)value getValue:&toSet];
((void (*)(id, SEL, SEL))imp)(self, setterSEL, toSet);
return;
}
}
[self shadySetValue:value forKey:key];
}
@end
// Copied from: https://stackoverflow.com/a/1638940/475052
void Swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
int main(int argc, char *argv[]) {
@autoreleasepool {
Swizzle([NSObject class], @selector(valueForKey:), @selector(shadyValueForKey:));
Swizzle([NSObject class], @selector(setValue:forKey:), @selector(shadySetValue:forKey:));
SEL selector = @selector(description);
MyObject *object = [[MyObject alloc] init];
object.mySelector = selector;
SEL fromProperty = object.mySelector;
NSString *fromPropertyString = NSStringFromSelector(fromProperty);
NSValue *fromKVCValue = [object valueForKey:@"mySelector"];
SEL fromKVC;
[fromKVCValue getValue:&fromKVC];
NSString *fromKVCString = NSStringFromSelector(fromKVC);
NSLog(@"fromProperty = %@ fromKVC = %@", fromPropertyString, fromKVCString);
object.myInt = 1;
NSNumber *myIntFromKVCNumber = [object valueForKey:@"myInt"];
int myIntFromKVC = [myIntFromKVCNumber intValue];
int myIntFromProperty = object.myInt;
NSLog(@"int from kvc = %d from propety = %d", myIntFromKVC, myIntFromProperty);
selector = @selector(class);
NSValue *selectorValue = [NSValue valueWithPointer:selector];
[object setValue:selectorValue forKey:@"mySelector"];
SEL afterSettingWithKVC = object.mySelector;
NSLog(@"after setting the selector with KVC: %@", NSStringFromSelector(afterSettingWithKVC));
[object setValue:@42 forKey:@"myInt"];
int myIntAfterSettingWithKVC = object.myInt;
NSLog(@"after setting the int with KVC: %d", myIntAfterSettingWithKVC);
}
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出演示了其装箱和拆箱功能:
2013-08-30 19:37:14.287 KVCSelector[69452:303] fromProperty = description fromKVC = description
2013-08-30 19:37:14.288 KVCSelector[69452:303] int from kvc = 1 from propety = 1
2013-08-30 19:37:14.289 KVCSelector[69452:303] after setting the selector with KVC: class
2013-08-30 19:37:14.289 KVCSelector[69452:303] after setting the int with KVC: 42
Run Code Online (Sandbox Code Playgroud)
调酒当然不是没有风险,所以要小心!
| 归档时间: |
|
| 查看次数: |
1994 次 |
| 最近记录: |