更新:
我将问题归结为根本无法在下面看到的类上使用键值编码
#import <Foundation/Foundation.h>
#import <GLKit/GLKit.h>
@interface CMTransformation : NSObject
@property(nonatomic) GLKVector3 position;
@property(nonatomic) GLKVector3 scale;
@property(nonatomic) GLKVector3 rotation;
@property(nonatomic) GLKVector3 anchor;
@property(nonatomic) GLKMatrix4 matrix;
- (GLKMatrix4)calculateMatrixWithParentTransformationCMTransformation *)parentTransformation;
@end
根据我的理解和经验,我应该能够将非 NSObject 作为 NSValue 提取出来,但是,我发现使用 KVC 语法无法访问这些项目(定义为联合):
CMTransformation* trans = [[CMTransformation alloc] init];
temp = [trans valueForKey"position"];
同样,如果我尝试访问底层变量:
CMTransformation* trans = [[CMTransformation alloc] init];
temp = [trans valueForKey"_position"];
这两个都抛出异常,因为找不到 key 。我在这里错过了什么?
上一个问题
我编写了一些代码,允许我使用诸如“transformation.position”之类的字符串访问(有点)任意结构
由于某种原因,当我尝试从 NSObject 读取属性时,代码在第二次跳转时停止工作。这里是
NSString* property = actionDetails[@"roperty"];
PropertyParts = [[property componentsSeparatedByString"."] mutableCopy];
int count = [PropertyParts count];
id current_object = initial_object;
for(int i = 0; i < count; i++)
{
NSString* current_part = PropertyParts[i];
current_object = [current_object valueForKey:current_part];
}
我已经尝试了所有可能的属性访问语法,包括 Property、property 和 _property。
这是自定义的 NSObject 声明
#import <Foundation/Foundation.h>
#import <GLKit/GLKit.h>
@interface CMTransformation : NSObject
@property(nonatomic) GLKVector3 position;
@property(nonatomic) GLKVector3 scale;
@property(nonatomic) GLKVector3 rotation;
@property(nonatomic) GLKVector3 anchor;
@property(nonatomic) GLKMatrix4 matrix;
- (GLKMatrix4)calculateMatrixWithParentTransformationCMTransformation *)parentTransformation;
@end
另外,在第一个循环之后,我可以看到调试器说 CMTransformation* 正在填充 currrent_object,所以我不知道为什么我不能访问它的属性?
Best Answer-推荐答案 strong>
您对实现 valueForUndefinedKey: 和 setValue:forUndefinedKey: 满意吗?
如果是这样,这可行:
union Test
{
CGFloat f ;
NSInteger i ;
};
@interface TestClass : NSObject
@property ( nonatomic ) union Test t ;
@end
@implementation TestClass
-(void)setValuenullable id)value forUndefinedKeynonnull NSString *)key
{
Ivar v = class_getInstanceVariable( [ self class ], [ [ NSString stringWithFormat"_%@", key ] UTF8String ] ) ;
if ( v )
{
char const * const encoding = ivar_getTypeEncoding( v ) ;
if ( encoding[0] == '(' ) // unions only
{
size_t size = 0 ;
NSGetSizeAndAlignment( encoding, &size, NULL ) ;
uintptr_t ptr = (uintptr_t)self + ivar_getOffset( v ) ;
[ (NSValue*)value getValuevoid*)ptr ] ;
return ;
}
}
objc_property_t prop = class_getProperty( [ self class ], [ key UTF8String ] ) ;
if ( prop )
{
char const * const encoding = property_copyAttributeValue( prop, "T" ) ;
if ( encoding[0] == '(' ) // unions only
{
objc_setAssociatedObject( self, NSSelectorFromString( key ), value, OBJC_ASSOCIATION_COPY ) ;
return ;
}
}
[ super setValue:value forUndefinedKey:key ] ;
}
-(nullable id)valueForUndefinedKeynonnull NSString *)key
{
Ivar v = class_getInstanceVariable( [ self class ], [ [ NSString stringWithFormat"_%@", key ] UTF8String ] ) ;
if ( v )
{
char const * const encoding = ivar_getTypeEncoding( v ) ;
if ( encoding[0] == '(' )
{
size_t size = 0 ;
NSGetSizeAndAlignment( encoding, &size, NULL ) ;
uintptr_t ptr = (uintptr_t)self + ivar_getOffset( v ) ;
NSValue * result = [ NSValue valueWithBytesvoid*)ptr objCType:encoding ] ;
return result ;
}
}
objc_property_t prop = class_getProperty( [ self class ], [ key UTF8String ] ) ;
if ( prop )
{
return objc_getAssociatedObject( self, NSSelectorFromString( key ) ) ;
}
return [ super valueForUndefinedKey:key ] ;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool
{
union Test u0 = { .i = 1234 } ;
TestClass * const testClass = [ TestClass new ] ;
[ testClass setValue:[ NSValue valueWithBytes:&u0 objCTypeencode( typeof( u0 ) ) ] forKey"t" ] ;
assert( testClass.t.i == 1234 ) ;
NSValue * const result = [ testClass valueForKey"t" ] ;
union Test u1 ;
[ result getValue:&u1 ] ;
assert( u1.i == 1234 ) ;
}
return 0;
}
(粘贴到你的main.m文件中试试)
关于ios - 在 Objective-C 中使用 KVC 读取联合属性,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/31006982/
|