首页 > 其他分享 >Objective-C语法property详解

Objective-C语法property详解

时间:2022-11-30 21:34:50浏览次数:34  
标签:property obj string NSObject 详解 Objective retain copy


1、简介: 

property是Objective-C的关键词,与@synthesize配对使用,用来让编译好器自动生成与数据成员同名的方法声明。@synthesize则是用来生成对应声明方法的实现。

 

1.1 property的语法格式:

@property (参数1,参数2)类型名字;

这里的参数,主要有以下三种:

setter/getter方法(assign/retain/copy)

读写属性(readwrite/readonly)

atomicity(nonatomic)

1.2 三种方式的使用

assign/retain/copy  代表赋值的方式。

 

readonly关键字代表setter不会被生成, 所以它不可以和 copy/retain/assign组合使用。

atomicity的默认值是atomic,读取函数为原子操作。

1.2.1 copy/reain/assign 在其中选择一个来确定属性的setter如何处理这个属性。NSObject对象采用这个中方式。

1.2.2 一些特别的Object比如NSSstring使用copy。

1.2.3 assign关键字代表setter直接赋值,而不是复制或者保留它。适用于基本数据类型,比如NSInteger和CGFloat,或者你并不直接拥有的类型,比如delegates。

2、如何使用property

1.1  没有property和有property的对比

在头文件定义 obj。在.m文件中使用

 



[cpp]  ​​view plain​​ ​​copy​​



  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSObject *obj;  
  6. }  
  7. @end  



[cpp]  ​​view plain​​ ​​copy​​



  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];      
  4.     self.obj = nil;、  
  5. }  


提示不可用。

Objective-C语法property详解_深入浅出Objective-C


加上property

 

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSObject *obj;  
  6. }  
  7. @property (nonatomic,retain) NSObject *obj;  
  8. @end  


编译能通过,运行,崩溃,提示错误 reason: '-[ViewController setObj:]: unrecognized selector sent to instance 0x6b6c480

 

那就是我们没事实现setter方法。

用@synthesize关键字实现getter 和setter。

在.m文件中

 


[cpp]  ​​view plain​​ ​​copy​​


  1. @implementation ViewController  
  2. @synthesize obj;  
  3. - (void)viewDidLoad  
  4. {  
  5.     [super viewDidLoad];  
  6.     self.obj = nil;  
  7. }  


运行,程序运行正常。说明setter 起作用了。

 

3、@property和@synthesize关键字 生成的代码

到底@property和@synthesize关键字生成了什么代码呢?我们自己实现getter 和setter也可以替代这些关键字。

把这两个关键字对应的代码注释掉

.h

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSObject *obj;  
  6. }  
  7. //@property (nonatomic,retain) NSObject *obj;  
  8. -(NSObject*)obj;  
  9. -(void)setObj:(NSObject*)newObj;  
  10. @end  


 

.m

 


[cpp]  ​​view plain​​ ​​copy​​


  1. @implementation ViewController  
  2. //@synthesize obj;  
  3. - (void)viewDidLoad  
  4. {  
  5.     [super viewDidLoad];  
  6.     self.obj = nil;  
  7. }  
  8.   
  9. -(NSObject*)obj{  
  10. return obj;  
  11. }  
  12. -(void)setObj:(NSObject*)newObj{  
  13. if(obj != newObj){  
  14.         [obj release];  
  15.         obj = [newObj retain];  
  16.     }  
  17. }  


再运行,也能正常启动。说明自己写的getter 和setter替代了property。

 

4、使用三种参数的对比

 

@property (nonatomic,retain)NSObject *obj;

@property (nonatomic,retain,readwrite) NSObject *obj;

readwrite是默认行为,所以这两行代码等价

 

 

@property (retain) NSObject *obj;

@property (atomic,retain) NSObject *obj;

atomic是默认行为,所以这两行代码是等价的。

 

 

@property(atomic,assign)int number;        

@property(atomic) int

@property int number;  

对int 来说,atomic assign都是默认行为,所以这三行是等价的。

 

@property  NSObject *obj;这样写行吗?不行的,报警告

Objective-C语法property详解_深入浅出Objective-C_02

只有int 等基础数据类型能这么写。对象必须加上赋值的类型。

 

@property  (retain) NSObject *obj;这样就没问题了。何时使用assign、何时使用retain、copy后面再讲。

5、retain和copy实验。

使用copy。

.h文件

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSString *string;  
  6. }  
  7. @property  (nonatomic, copy) NSString *string;  
  8. @end  


 

 

.m文件

 


[cpp]  ​​view plain​​ ​​copy​​


  1. @synthesize string;  
  2. - (void)viewDidLoad  
  3. {  
  4.     [super viewDidLoad];  
  5.       
  6. "abcd"];  
  7. "str_Point:%p  %@  retainCount:%d", str, str, [str retainCount]);  
  8.     self.string = str;  
  9. "string_Point:%p  %@  retainCount:%d", string, string, [string retainCount]);  
  10. }  


打印结果

 

 

 

2012-07-19 20:41:44.853 TestProject1[1213:f803] str_Point:0x6a8e0b0  abcd  retainCount:1

2012-07-19 20:41:44.854 TestProject1[1213:f803] string_Point:0x6a8e0b0  abcd  retainCount:2

内存地址是一样的,不是想其他文字所写的那样,拷贝了一份内存,这里用copy也是浅拷贝。retain也+1

 

使用retain

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSString *string;  
  6. }  
  7. @property  (nonatomic, retain) NSString *string;  
  8. @end  


打印结果是:

 

2012-07-19 20:42:08.113 TestProject1[1230:f803] str_Point:0x6d3b8f0  abcd  retainCount:1

2012-07-19 20:42:08.114 TestProject1[1230:f803] string_Point:0x6d3b8f0  abcd  retainCount:2

结果和上面copy一样。

 

注意:在IOS5之后,加入了Automatic Reference Counting (ARC),iOS5中新加了关键字有strong, weak, unsafe_unretained。

标签:property,obj,string,NSObject,详解,Objective,retain,copy
From: https://blog.51cto.com/u_3457306/5900557

相关文章

  • Objective-C语法之KVC的使用
    除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性。下图来自苹果官网: 如何使用KVC存取对象属性呢?看个示例1、使用KVC定义......
  • HTTP2 协议长文详解
    一、HTTP2简介HTTP2是一个超文本传输协议,它是HTTP协议的第二个版本。HTTP2主要是基于google的SPDY协议,SPDY的关键技术被HTTP2采纳了,因此SPDY的成员全程参与......
  • 指针详解(day19)
    5.函数指针释义:指向函数的指针。函数指针的创建实例intAdd(intx,inty){intz;z=x+y;returnz;}intmain(){inta=1;intb=2;printf("\n%d\n",Add(a,......
  • RocketMQ 的消费者类型详解与最佳实践
    作者:凌楚在RocketMQ5.0中,更加强调了客户端类型的概念,尤其是消费者类型。为了满足多样的RocketMQ中一共有三种不同的消费者类型,分别是PushConsumer、SimpleConsumer和......
  • RocketMQ 的消费者类型详解与最佳实践
    作者:凌楚在RocketMQ5.0中,更加强调了客户端类型的概念,尤其是消费者类型。为了满足多样的RocketMQ中一共有三种不同的消费者类型,分别是PushConsumer、SimpleConsumer......
  • 博奥智源公司:微信代运营思路详解
    1.专人运营。运营方需安排至少1名专职编辑负责收集、挖掘区文化旅游资源,策划原创微信向市民、游客推荐优质文旅线路和人文故事,并有专人负责审核、校对、采稿、拍摄、美编、......
  • linux ls命令详解
      参数含义-a all,  显示所有文件及目录(.开头的隐藏文件也会列出)-A 同-a,但不列出“.”(目前目录)及“…”(父目录)-l 以长格式显示目录下的内容列表,包......
  • 2022最全Hbuilder打包成苹果IOS-App的详解
     本文相关主要记录一下使用Hbuilder打包成苹果IOS-App的详细步骤。介绍一下个人开发者账号:再说下什么是免费的苹果开发者账号,就是你没交688年费的就是免费账号,如果你......
  • linux下awk命令详解
    awk是行处理器:相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息awk处理过程: 依次对每一行进行处理,然后输出awk命令......
  • Docker 网络详解
    这一篇文章将介绍Docker中的网络情况,欢迎大家学习讨论。docker网络官网https://docs.docker.com/network/计算机网络模型OSI七层模型TCP/IP四层模型Linux中网卡查看网卡[网......