美文网首页
KVC 如何给非对象属性赋值

KVC 如何给非对象属性赋值

作者: 等一个最好的自己 | 来源:发表于2020-12-01 10:42 被阅读0次

    kvc给对象属性赋值大家肯定已经很熟悉了,但是怎么给非对象属性赋值呢,现在举例给以结构体为属性并赋值为例:

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    typedef struct {
        float x,y,z;
    }Floats;
    @interface TestObject : NSObject
    @property(nonatomic)Floats testFloat;
    @end
    
    NS_ASSUME_NONNULL_END
    
    TestObject *testObject = [[TestObject alloc]init];
        //赋值
        Floats testFolat = {1.0,2.0,3.0};
        NSValue *value = [NSValue valueWithBytes:&testFolat objCType:@encode(Floats)];
        [testObject setValue:value forKey:@"testFloat"];
    
        //取值
        NSValue *value1 = [testObject valueForKey:@"testFloat"];
        Floats floats;
        [value1 getValue:&floats];
        NSLog(@"%lf-%lf-%lf",floats.x,floats.y,floats.z);
    

    打印结果:1.000000-2.000000-3.000000

    相关文章

      网友评论

          本文标题:KVC 如何给非对象属性赋值

          本文链接:https://www.haomeiwen.com/subject/bznjwktx.html