zoukankan      html  css  js  c++  java
  • Objective-C NSValue类的常用方法

    NSValue类是OC中用来存储任意值的容器,是NSNumber的父类,NSNumber类是用来封装基本数据类型的

    初始化方法:

    - (instancetype)initWithBytes:(const void *)value
                         objCType:(const char *)type
    //便利构造器方法
    + (NSValue *) valueWithBytes:(const void *)value
                       objCType:(const char *)type

    第一个参数为要封装的变量的地址,第二个参数用来描述该数据类型的字符串,可以用@encode编译器指令自动生成该字符串

    用法:

    //自定义结构体类型Student
    typedef struct student{
            char name[50];
            char sex[2];
            NSInteger age;
            NSInteger grade;
            char number[50];
        }Student;
    //创建结构体stu
    Student stu = {"smithjackyson","男",23,98,"3112006304"};
    //创建NSValue实例value存储stu
    NSValue *value = [[NSValue alloc] initWithBytes:&stu objCType:@encode(Student)];
    NSValue *value1 = [NSValue valueWithBytes:&stu objCType:@encode(Student)];

    获取value中存储的对象如下:

    //定义一个新结构体变量stu1
    Student stu1;
    //将value中存储的结构体赋值给stu1
    [value getValue:&stu1];

    判断两个NSvalue存储的对象是否相同

    - (BOOL)isEqualToValue:(NSValue *)aValue

    存储各种类型的便利构造器和属性,以及获取对应值的方法:

    + (NSValue *)valueWithRange:(NSRange)range
    @property(readonly) NSRange rangeValue
    
    + (NSValue *)valueWithCGPoint:(CGPoint)point
    - (CGPoint)CGPointValue
    
    + (NSValue *)valueWithCGVector:(CGVector)vector
    - (CGVector)CGVectorValue
    
    + (NSValue *)valueWithCGSize:(CGSize)size
    - (CGSize)CGSizeValue
    
    + (NSValue *)valueWithCGRect:(CGRect)rect
    - (CGRect)CGRectValue
    
    + (NSValue *)valueWithPointer:(const void *)aPointer
    @property(readonly) void *pointerValue
    
    + (NSValue *)valueWithNonretainedObject:(id)anObject
    @property(readonly) id nonretainedObjectValue
    
    + (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform
    - (CGAffineTransform)CGAffineTransformValue
    
    + (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets
    - (UIEdgeInsets)UIEdgeInsetsValue
    
    + (NSValue *)valueWithUIOffset:(UIOffset)insets
    - (UIOffset)UIOffsetValue
    
    + (NSValue *)valueWithCATransform3D:(CATransform3D)aTransform
    @property(readonly) CATransform3D CATransform3DValue
    
    + (NSValue *)valueWithCMTime:(CMTime)time
    @property(readonly) CMTime CMTimeValue
    
    + (NSValue *)valueWithCMTimeRange:(CMTimeRange)timeRange
    @property(readonly) CMTimeRange CMTimeRangeValue
    
    + (NSValue *)valueWithCMTimeMapping:(CMTimeMapping)timeMapping
    @property(readonly) CMTimeMapping CMTimeMappingValue
    
    + (NSValue *)valueWithMKCoordinate:(CLLocationCoordinate2D)coordinate
    @property(readonly) CLLocationCoordinate2D MKCoordinateValue
    
    + (NSValue *)valueWithMKCoordinateSpan:(MKCoordinateSpan)span
    @property(readonly) MKCoordinateSpan MKCoordinateSpanValue
    
    + (NSValue *)valueWithSCNVector3:(SCNVector3)vector
    @property(nonatomic, readonly) SCNVector3 SCNVector3Value
    
    + (NSValue *)valueWithSCNVector4:(SCNVector4)vector
    @property(nonatomic, readonly) SCNVector4 SCNVector4Value
    
    + (NSValue *)valueWithSCNMatrix4:(SCNMatrix4)
    @property(nonatomic, readonly) SCNMatrix4 SCNMatrix4Value
    

    转载请注明:作者SmithJackyson

  • 相关阅读:
    IDEA04 工具窗口管理、各种跳转、高效定位、行操作、列操作、live template、postfix、alt enter、重构、git使用
    Maven01 环境准备、maven项目结构、编译/测试/打包/清除、安装、
    SpringBoot31 整合SpringJDBC、整合MyBatis、利用AOP实现多数据源
    Docker03 Docker基础知识、Docker实战
    [leetcode数组系列]2三数之和
    [leetcode数组系列]1 两数之和
    时间复杂度总结
    《将博客搬至CSDN》
    5 系统的软中断CPU升高,一般处理办法?
    python数据分析5 数据转换
  • 原文地址:https://www.cnblogs.com/smithjackyson/p/5059250.html
Copyright © 2011-2022 走看看