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
网友评论