当前位置:网站首页>OC--包装类和处理对象
OC--包装类和处理对象
2022-07-25 09:21:00 【山河丘壑】
文章目录
前言
浅浅说一下包装类和对象的相关内容
一、包装类
1.什么是对象?
OC是面向对象的语言,但C语言所包含的基本类型(int,short,float,double)等都不是对象。
他们也没有对象的特性——没有属性,方法可以被调用。
那么什么是对象呢?
OC提供了NSValue,NSNumber来封装C语言的基本类型,这样就可以让他们具有面向对象的特征。
2.包装类
- NSInteger:大致等于long类型整数
- NSUInteger:大致等于unsigned long类型整数
- CGFloat:64位平台上相当于double,32位平台上相当于float
这三个类依然只是基本类型而不是包装类
为了更好的兼容不同的平台,建议需要定义整形变量时使用NSInteger,NSUInteger,定义浮点型变量时,使用CGFloat
二、NSValue和NSNumber
1.是什么?
NSValue和NSNumber都是包装类,NSValue是NSNumber的父类
NSValue更为通用,可以用于包装单个shorting, long, float, char,zhizhen,对象id等数据类型,通过包装该类,就可以把shorting ,long, float, char,指针等添加到NSArray,NSSet等集合(集合的元素必须是对象)。
2.怎么用?
(1)+numberWithXxx:将特定类型的值包装成为NSNumber
(2)-initWithXxx:先创建一个NSNumber对象,在用一个基本类型的值初始化NSNumber
(3)-xxxValue:返回该NSNumber对象包装的基本类型的值。
#import<Foundation/Foundation.h>
int main(int argc, char * argv[]){
@autoreleasepool {
//将int类型的值包装成为NSNumber对象
NSNumber* num = [NSNumber numberWithInt: 20];
//将double类型的值包装成为NSNumber对象
NSNumber* de = [NSNumber numberWithDouble: 3.4];
NSLog(@"%d", [num intValue]);
NSLog(@"%g", [de doubleValue]);
//用initWithXxx执行初始化
NSNumber* ch = [[NSNumber alloc]initWithChar: 'j'];
//字母j的ASCLL码为106,所以输出106
NSLog(@"%@", ch);
}
}
运行结果如下:

三、处理对象
OC对象都是NSObject子类的实例,doily直接调用该类中定义的方法。这些方法都提供了处理OC对象的通用方法。
1.打印对象和description
Person类的接口
#import<Foundation/Foundation.h>
@interface Person: NSObject
@property (nonatomic,copy)NSString* name;
-(id)initWithName: (NSString*)namel;
-(void)info;
@end
Person类的实现
#import"heihei.h"
@implementation Person
@synthesize name = _name;
-(id) initWithName: (NSString*)name{
if(self = [super init]){
self.name = name;
}
return self;
}
-(void)info{
NSLog(@"这个人叫:%@", self.name);
}
@end
测试
#import<Foundation/Foundation.h>
#import"heihei.h"
int main(){
@autoreleasepool {
Person* p = [[Person alloc]initWithName:@"yangyang"];
NSLog(@"%@", [p description]);
}
}
这里是代码结果
这个输出结果是怎么来的呢?NSLog()函数只能在控制台输出字符串,而Person实例是内存中存在的一个对象,怎么才能直接转换成字符串输出呢?
其实,使用NSLog()函数输出Person时,输出的是Person的对象的description方法的返回值。
即System.out.println(p )完全等于System.out.println([p description])
四、自我描述
1.是什么?
description方法一个自我描述的方法,当我们直接打印该对象时,会输出该对象的“自我描述”信息,用以告诉外界该对象具有的状态信息。description方法返回的值并不能实现“自我描述”,因此如果想让他实现自我描述,必须重写NSObject的description方法。
2.怎样实现?
Apple类的接口部分
#import<Foundation/Foundation.h>
@interface Apple: NSObject
@property(nonatomic, copy)NSString* color;
@property(nonatomic, assign)double weight;
-(id) initWithColor: (NSString*)color weight: (double)weight;
@end
Apple类的实现部分
#import"heihei.h"
@implementation Apple
@synthesize color = _color;
@synthesize weight = _weight;
-(id)initWithColor: (NSString *)color weight: (double)weight{
if(self = [super init]){
self.color = color;
self.weight = weight;
}
return self;
}
-(NSString*)description{
return [NSString stringWithFormat: @"Apple[_color=%@,_weight=%g]",
self.color, self.weight];
}
@end
测试
#import<Foundation/Foundation.h>
#import"heihei.h"
int main(){
@autoreleasepool {
Apple* a = [[Apple alloc]initWithColor:@"红色" weight:5];
NSLog(@"%@", a);
}
}
代码结果如下
我们通过重写Apple类的description方法,如愿得到了我们想要的“自我描述”
五、==和isEqual
1.是什么?
OC中测试两个变量是否相等的方法
==
如果两个变量是基本类型的变量,且都是数值型(不一定要求数据类型严格相同)只要两个变量的值相等,使用==就返回1
如果是指针类型的变量,他们指向同一个对象时,==判断就返回1
==比较的是两个字符串的地址
2.怎么用?
#import<Foundation/Foundation.h>
#import"heihei.h"
int main(){
@autoreleasepool {
int it = 65;
float f1 = 65.0f;
//65和65.0是否相等? 相等
NSLog(@"%d", (it == f1));
char ch = 'A';
//65和‘A'是否相等? 相等
NSLog(@"%d", (it == ch));
NSString* str1 = [NSString stringWithFormat: @"hello"];
NSString* str2 = [NSString stringWithFormat: @"hello"];
NSLog(@"%d", (str1 == str2));
//str1 isEqual str2 相等
NSLog(@"%d",[str1 isEqual:str2]);
//NSDate与NSString没有继承关系,所以会报错
NSLog(@"%d",[NSDate new] == [NSString new]);
}
}
这里是NSDate与NSString判断时的报错
这里是代码运行结果
NSString已经重写了NSObject的isEqual方法,只要两个字符串中包含的字符序列相同,通过isEqual比较就返回真。
我们再来重新测试一下这个代码
#import<Foundation/Foundation.h>
int main(int argc, char* argv[])
{
@autoreleasepool{
NSString* str1 = [NSString stringWithFormat:@"娜娜"];
NSString* str2 = [NSString stringWithFormat:@"娜娜"];
NSLog(@"%p", str1);
NSLog(@"%p", str2);
NSLog(@"str1和str2是否相等?%d", (str1 == str2));
NSLog(@"str1是否isEqual:str2?%d", [str1 isEqual: str2]);
}
}

重写isEqual方法就是自定义的相等标准
isEqual
1,自反性:[x isEqual: x]=1
2.对称性:如果[x isEqual: y]=1,则[y isEqual: x]=1
3.传递性:如果[x isEqual: y]=1,[y isEqual: z]=1,则[x isEqual: z]=1
4.对于任何不是nil的x,[x isEqual: nil]=0
NSString不仅重写了isEqual方法,用于判断两个字符串的序列是否相等,还定义了一个isEqualToString:方法,用于判断当前字符串于另一个字符串序列是否相等。
六、==
在上面的代码中我们看到,使用NSString*str=[NSString
stringWithFormat:@“hello”];时,用==判断两个字符串是否相等时,返回了1
这里要注意,这里的字符串只能是英文模式并且长度不能超过九
当字符串含有q时,长度不能超过7
#import<Foundation/Foundation.h>
int main(int argc, char* argv[])
{
@autoreleasepool{
NSString* str1 = [NSString stringWithFormat: @"hellowwwww"];
NSString* str2 = [NSString stringWithFormat: @"hellowwwww"];
NSLog(@"%p", str1);
NSLog(@"%p", str2);
NSLog(@"str1和str2是否相等?%d", (str1 == str2));
}
}
这是当字符串长度大于9时的结果
这是当字符串为中文时的结果
#import<Foundation/Foundation.h>
int main(int argc, char* argv[])
{
@autoreleasepool{
NSString* str1 = [NSString stringWithFormat:@"娜娜"];
NSString* str2 = [NSString stringWithFormat:@"娜娜"];
NSLog(@"%p", str1);
NSLog(@"%p", str2);
NSLog(@"str1和str2是否相等?%d",(str1 == str2));
}
}

当我们赋值给字符串时,结果又不一样了。
这是英文的长度小于等于9的代码:
#import<Foundation/Foundation.h>
int main(int argc, char* argv[])
{
@autoreleasepool{
NSString* str1 = @"hello";
NSString* str2 = @"hello";
NSLog(@"%p", str1);
NSLog(@"%p", str2);
NSLog(@"str1和str2是否相等?%d", (str1 == str2));
}
}

这里是英文的长度大于9的代码:
#import <Foundation/Foundation.h>
#import"0608.h"
int main(){
@autoreleasepool {
NSString* str1=@"qqqqqqqqqqq";
NSString* str2=@"qqqqqqqqqqq";
NSLog(@"%d",(str1 == str2));
NSLog(@"%d",[str1 isEqual:str2]);
}
}
结果如下:
这里是含有q且长度小于9的代码
#import"0608.h"
int main(){
@autoreleasepool {
NSString* [email protected]"qqqqqq";
NSString* [email protected]"qqqqqq";
NSLog(@"%d",(str1 == str2));
NSLog(@"%d",[str1 isEqual:str2]);
}
}
代码结果:
这里是当字符串为中文字符时的代码:
#import <Foundation/Foundation.h>
#import"0608.h"
int main(){
@autoreleasepool {
NSString* str1=@"挪挪";
NSString* str2=@"挪挪";
NSLog(@"%d",(str1 == str2));
NSLog(@"%d",[str1 isEqual:str2]);
}
}

边栏推荐
- Flask SSTI注入学习
- main函数的一些操作
- PHP介绍
- Jar包在阿里云服务器起起来了,安全组也开通了,但postman仍跑不通怎么办
- 什么是脑裂问题?
- Publish Yum private server using nexus3 (offline intranet)
- 类(2) 和 协议
- matplotlib数据可视化三分钟入门,半小时入魔?
- Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground
- *6-1 CCF 2015-03-2 数字排序
猜你喜欢
随机推荐
*6-3 节约小能手
[De1CTF 2019]SSRF Me
*7-2 CCF 2015-09-2 日期计算
OC--对象复制
Redis set 结构命令
Unable to start debugging on the web server, the web server failed to find the requested resource
Neural network method -- Boston house price (regression problem)
粗柳簸箕细柳斗,谁嫌爬虫男人丑 之 异步协程半秒扒光一本小说
数据控制语言(DCL)
[GKCTF 2021]easynode
¥1-2 例2.2 将两个集合的并集放到线性表中
最短路问题 Bellman-Ford(单源最短路径)(图解)
CoreData存储待办事项
梦想启航(第一篇博客)
~4.1 剑指 Offer 05. 替换空格
Redis数据库基础
机器学习 —— Sklearn包中StandardScaler()、transform()、fit()的详细介绍
初始Flask以及简单地上手应用
Detailed explanation of the use of nanny scanner class
对象初始化









![[GYCTF2020]Node Game](/img/8d/7e6c2fb2a0359298fbcc1cd8544710.png)