一个NSValue对象是用来存储一个C或者Objective-C数据的简单容器。它可以保存任意类型的数据,比如int,float,char,当然也可以是指pointers, structures, and object ids。
初始化
- (instancetype)initWithBytes:(const void *)value objCType:(const char *)type
参数value为数据的指针,参数type 为数据类型的编码,通过@encode(数据类型)获得。
举例
struct student
{
int age;
char name[16];
};
char *type=@encode(struct student);
构造
+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;
+ (NSValue *)value:(const void *)value withObjCType:(const char *)type;
获取
- (void)getValue:(void *)value;
获取保存的数据
@property (readonly) const char *objCType
获取数据类型的编码
相等
- (BOOL)isEqualToValue:(NSValue *)value;
判断是否相等
其他
+ (NSValue *)valueWithNonretainedObject:(nullable id)anObject;
@property (nullable, readonly) id nonretainedObjectValue;
保存/获取对象
+ (NSValue *)valueWithPointer:(nullable const void *)pointer;
@property (nullable, readonly) void *pointerValue;
保存/获取指针