@property和@synthesize关键字的介绍

本文主要介绍@property和@synthesize关键字的使用,以及两者在Xcode4.4前后的区别。


Xcode4.4之前,@property不会生成属性,只能生成setter和getter方法的声明。必须配合在.m文件中使用@synthesize才能生成setter和getter方法实现。

1)在 .h文件中使用@property。首先需要在 { } 中定义带下划线的实例变量,然后使用@property来生成set和get方法声明:

<p style="color: rgb(0, 132, 0); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">@interface</span> Person : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSObject</span></p><p style="color: rgb(0, 132, 0); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">{</p><p style="color: rgb(0, 132, 0); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>//<span style="line-height: normal; font-family: 'PingFang SC';">实例变量</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="color: rgb(112, 61, 170);">    NSString</span><span style="color:#008400;"> </span><span style="color:#333333;">*_name</span><span style="color:#008400;">;</span></p><p style="color: rgb(0, 132, 0); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"></p><p style="color: rgb(0, 132, 0); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p style="color: rgb(187, 44, 162); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">@property<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *name; </span></p><p style="color: rgb(187, 44, 162); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">
</span></p><p style="color: rgb(187, 44, 162); margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);">@end</p>

上面的代码相当于:

<pre name="code" class="objc" style="color: rgb(0, 132, 0); font-size: 14px;"><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"></p><pre name="code" class="objc"><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="color: rgb(187, 44, 162);">@interface</span> Person : <span style="color: rgb(112, 61, 170);">NSObject</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"><span style="color: rgb(0, 0, 0);">    </span>//<span style="line-height: normal; font-family: 'PingFang SC';">实例变量</span></p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">    <span style="color: rgb(112, 61, 170);">NSString</span> *_name;</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">    <span style="color: rgb(187, 44, 162);">int</span> _weight;</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">}</p>
// 相当于声明了setter和getter方法

-(void)setName:(NSString *)name;

-(NSString *)name;


@end

 
 

*)这里需要注意的是,@property后面的变量名不要加上下划线 _,否则生成的setter和getter方法名也会带上下划线,比如:

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);">@property<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *_name;</span></p>
上面的代码相当于:

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)set_name:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)_name;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)_name;</p>

2)在 .m 文件中,使用@synthesize来对setter和getter方法进行实现:

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);">@implementation<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> Person</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);">@synthesize<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> name = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_name</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">;</span></p>

@end

Tip:上述代码中,如果在.h文件中没有定义实例变量_name,则系统会.m文件中自动生成私有变量_name。

上面的代码相当于:
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">@implementation</span><span style="font-family: Menlo; font-size: 14px; white-space: pre;"> Person</span>
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)setName:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)name{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_name</span> = name;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)name{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_name</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">@end</span>
*)这里 需要注意 的是,如果@synthesize不指名赋值的变量的话,则会默认以 = 左边的变量进行读取操作:

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);">@synthesize<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> name</span></p>

Tip:此时.m文件中生成的实例变量为不带下划线的 name

上面的代码相当于:

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)setName:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)name {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">name</span> = name;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)name {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_name</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>



Xcode4.4之后,就是我们现在经常用,熟悉不过的@property单独使用的方式了,即不仅会生成属性,也会生成setter和getter方法的声明和实现:

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);">@interface<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> Person : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSObject</span></p>

@property NSString *name;

@end

上面的代码相当于:.h文件声明了setter和getter方法,.m文件中实现了两个方法:

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">@interface</span><span style="font-family: Menlo; font-size: 14px; white-space: pre;"> Person : </span><span style="font-family: Menlo; font-size: 14px; white-space: pre; color: rgb(112, 61, 170);">NSObject</span>
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span>)setName:(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)name;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *)name;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">@end</span></p>
<pre name="code" class="objc" style="color: rgb(51, 51, 51);font-size:14px; font-family: 'PingFang SC';"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"><span style="color: rgb(187, 44, 162);">@implementation</span> Person</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span> *_name;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; line-height: normal; font-family: Menlo;">}</p>

 

-(void)setName:(NSString *)name{

    _name = name;

}

-(NSString *)name{

    return _name;

}

@end

需要注意的是:如果用@property构造属性后,不能在实现中同时重写setter和getter方法,否则系统将不会自动生成带下划线的属性了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值