当前位置:网站首页>OC-NSNumber and NSValue are generally used for boxing and unboxing
OC-NSNumber and NSValue are generally used for boxing and unboxing
2022-08-02 07:48:00 【Classmate Peng, she is at the same table】
NSNumber
装箱:将值类型转换成引用类型
拆箱:将引用类型转换成值类型
int a = 1; This is the value type It should be some default type
string a;或者NSNumberThis is the reference type Should be able to say yesNSObject
装箱
NSNumber *num1 = [[NSNumber alloc]initWithInt:20];
NSLog(@"%@ %@",num1,[num1 class]);//20 __NSCFNumber
NSNumber *num2 = [[NSNumber alloc]initWithChar:'a'];
NSLog(@"%@",num2);//97 This is to convert the letters into ascll码
NSNumber *num3 = @300;
NSLog(@"%@",num3);//300
int a = 29;
NSNumber *num5 = @(a);
NSLog(@"%@",num5);//5
拆箱
通过intValue floatValue拆箱
NSNumber *num3 = @300;
NSLog(@“%d”,num3 intValue);//300
NSNumber *num3 = @300.0;
NSLog(@“%f”,num3 floatValue);//300.0
NSValue(I don't understand)
typedef struct
{
int age;
char* name;
chat sex;
} SPerson
Sperson p;
p.name = "Yang";
p.age = 10;
p.sex = 'w';
//Convert a struct toNSValue对象
NSValue* value = [NSValue valueWithBytes:&p objCType:@encode(SPerson)];
SPerson p2;
//将一个NSValueObjects are converted to structs
[value getValue:&p2];
NSLog(@"%s",p2.name);
NSLog(@"%d",p2.age);
NSLog(@"%c",p2.sex);
NSRange range = {
12,2};
//Commonly used structure packing
NSValue *rvalue = [NSValue valueWithRange:range];
//rangeValue将NSRange拆箱
NSLog(@"%@",NSStringFromRange([rvalue rangeValue]));
边栏推荐
猜你喜欢
随机推荐
C#重点问题之Struct和Class的异同
反射课后习题及做题记录
实例028:递归求等差数列
论文阅读 (64):Weakly-supervised Video Anomaly Detection with Robust Temporal Feature Magnitude Learning
如何设计静态资源缓存方案
修改apt-get源为国内镜像源
查看僵尸进程
(2022牛客多校五)C-Bit Transmission(思维)
使用hutool做本地缓存的工具类
海缆探测仪TSS350(二)
ADS通信--倍福PLC和C#TextBox控件实现数据绑定的方法
MySQL-FlinkCDC-Hudi实时入湖
【ROS基础】rosbag 的使用方法
交换网络----三种生成树协议
Redis 常用命令和基本数据结构(数据类型)
SQL server 2014 怎么一次性导出多个查询结果?
Go 实现分布式锁
optional
带手续费买卖股票的最大利益[找DP的状态定义到底缺什么?]
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】