• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 Objective-C 中使用 KVC 读取联合属性

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:24:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

更新:

我将问题归结为根本无法在下面看到的类上使用键值编码

#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-推荐答案


您对实现 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap