当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property 和@synthesize属性,@property用在 .h 头文件中用作声明,@synthesize用在.m 文件中用于实现。

如下,新建一个基于“Command Line Tool”的项目,名为“property”,再新建一个Student类,

传统的写法是:

Student.h

  1. //
  2. //  Student.h
  3. //  property
  4. //
  5. //  Created by Rio.King on 13-8-25.
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface Student : NSObject
  10. {
  11. int age;
  12. int no;
  13. }
  14. //age的getter和setter方法声明
  15. - (int)age;
  16. - (void)setAge:(int)newAge;
  17. //no的getter和setter方法声明
  18. - (int)no;
  19. - (void)setNo:(int)newNo;
  20. @end

Student.m

  1. //
  2. //  Student.m
  3. //  property
  4. //
  5. //  Created by Rio.King on 13-8-25.
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.
  7. //
  8. #import "Student.h"
  9. @implementation Student
  10. //age的getter和setter方法的实现
  11. - (int)age
  12. {
  13. return age;
  14. }
  15. -(void)setAge:(int)newAge
  16. {
  17. age = newAge;
  18. }
  19. //no的getter和setter方法的实现
  20. - (int)no
  21. {
  22. return no;
  23. }
  24. - (void)setNo:(int)newNo
  25. {
  26. no = newNo;
  27. }
  28. @end

main.m

  1. //
  2. //  main.m
  3. //  property
  4. //
  5. //  Created by Rio.King on 13-8-25.
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "Student.h"
  10. int main(int argc, const char * argv[])
  11. {
  12. @autoreleasepool {
  13. // insert code here...
  14. Student *stu = [[Student alloc] init];
  15. stu.age = 100;//这句相当于setter方法
  16. NSLog(@"age is %i", stu.age);//这里的 stu.age 相当于getter方法
  17. [stu release];
  18. }
  19. return 0;
  20. }

------------------------------------------------------------------------------------------------------------------------

用@property和@synthesize的写法是:

Student.h

  1. //
  2. //  Student.h
  3. //  property
  4. //
  5. //  Created by Rio.King on 13-8-25.
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface Student : NSObject
  10. {
  11. int age;
  12. int no;
  13. }
  14. //当编译器遇到@property时,会自动展开成getter和setter的声明
  15. @property int age;
  16. @property int no;
  17. @end

Student.m

  1. //
  2. //  Student.m
  3. //  property
  4. //
  5. //  Created by Rio.King on 13-8-25.
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.
  7. //
  8. #import "Student.h"
  9. @implementation Student
  10. //@synthesize 会自动生成getter和setter的实现
  11. //@synthesize 默认会去访问age,no,height同名的变量,,
  12. //如果找不到同名的变量,会在内部自动生成一个私有同名变量age,no,height,,
  13. //因此Student.h 中的这几个变量也可以省略不写。
  14. @synthesize age,no;
  15. @end

main.m

  1. //
  2. //  main.m
  3. //  property
  4. //
  5. //  Created by Rio.King on 13-8-25.
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "Student.h"
  10. int main(int argc, const char * argv[])
  11. {
  12. @autoreleasepool {
  13. // insert code here...
  14. Student *stu = [[Student alloc] init];
  15. stu.age = 100;
  16. NSLog(@"age is %i", stu.age);
  17. [stu release];
  18. }
  19. return 0;
  20. }

几点说明:

1.在Xcode4.5及以后的版本中,可以省略@synthesize ,编译器会自动帮你加上getter 和 setter 方法的实现,并且默认会去访问

_age这个成员变量,如果找不到_age这个成员变量,会自动生成一个叫做 _age的私有成员变量。

2.视频教学中建议变量名用"_"前缀作为开头,但我看big Nerd 那本书里是不用的,个人也比较习惯 big Nerd 的那种写法,所以变量名就不加前缀了。Y^o^Y

摘自:http://blog.csdn.net/chaoyuan899/article/details/10310719

ios的@property属性和@synthesize属性(转)的更多相关文章

  1. ios的@property属性和@synthesize属性

    当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property 和@synthesize属性,@property用在 .h ...

  2. IOS中@property的属性weak、nonatomic、strong、readonly等介绍

    iOS开发中@property的属性weak nonatomic strong readonly等介绍 //property:属性://synthesize:综合; @property与@synthe ...

  3. iOS 中@property() 括号中,可以填写的属性?

    通过@property 和 @synthesize 属性可以简化设置器(set)和访问器(get) 的代码. 在@property 中又有那些属性呢? readwrite 默认 readonly 只读 ...

  4. (iOS)关于@property和@synthesize的理解(原创)

    开始学习ios的时候,就对一些objc的语法不理解,就比如@property和@synthesize,之前都是记住然后照着用,但是写的代码多了,对objc和ios有了一些理解,再加上最近用MRC,所以 ...

  5. iOS对UIViewController生命周期和属性方法的解析

    目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...

  6. 【iOS开发】iOS对UIViewController生命周期和属性方法的解析

    iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...

  7. iOS UIView控件的常用属性和方法的总结

    一 UIVIew 常见属性1.frame 位置和尺寸(以父控件的左上角为原点(0,0))2.center 中点 (以父控件的左上角为原点(0,0))3.bounds 位置和尺寸(以自己的左上角为原点 ...

  8. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...

  9. 利用@property实现可控的属性操作

    利用@property实现可控的属性操作 Python中没有访问控制符, 不像java之类的 public class Person{ private int x public int getAge( ...

随机推荐

  1. HTML学习3---排版标记

    上节,我们学习了boda常用的属性以及HTML的一些标记,但是图显示的效果却不是那么的好看. 原因就是没有排版好,我们这次使用居中来使这个页面更好看一点,顺便多加入几个别的标记. HTML排版标记 ( ...

  2. 送人玫瑰,手留余香——2015年技术分享交流小结

    飞测说:分享让我们更加团结,交流让我们更加凝聚,送人玫瑰,手留余香,更多分享交流也让自己成长的更加完善,2015年已经过去了好几个月,今天刚好整理了下我们科大讯飞武汉测试团队技术分享交流的这块,顺便做 ...

  3. .NET CORE微服务实践

    .NET CORE微服务实践 https://www.cnblogs.com/zengqinglei/p/9570343.html .NET CORE 实践部署架构图 实践源码:https://git ...

  4. RabbitMQ引入

    引入MQ话题 可能很多人有疑惑:MQ到底是什么?哪些场景下要使用MQ? 前段时间安装了RabbitMQ,现在就记录下自己的学习心得吧.首先看段程序: class Program { static vo ...

  5. centos6.5下tomcat安装

    1.安装JDK 安装:rpm –ivh jdk-7u5-linux-i586.rpm2.配置Tomcat 解压:tar -zxvf apache-tomcat-8.0.11.tar.gz 移动到/us ...

  6. 适合Centos Web服务器的iptables规则

    适合Centos Web服务器的iptables规则IPT="/sbin/iptables"$IPT --delete-chain$IPT --flush$IPT -P INPUT ...

  7. oracle之 Got minus one from a read call 与 ORA-27154: post/wait create failed

    在部署应用的时候,有时候应用可以直接启动,但偶尔应用却无法启动,报错信息是: java.sql.SQLRecoverableException: IO Error: Got minus one fro ...

  8. Git自动换行符

    http://blog.csdn.net/jonathan321/article/details/51988242?locationNum=2 不同的操作系统有不同的换行符格式,跨平台协作时需要考虑版 ...

  9. RK3288 红外遥控器增加自定义按键

    转载请注明出处:https://www.cnblogs.com/lialong1st/p/10071557.html CPU:RK3288 系统:Android 5.1 1.在 dts 中增加红外遥控 ...

  10. CSS背景图像的简单响应

    本文设有很多,最理想的解决方案,响应图像只是其中之一.我们建议您查看不同的方法,然后再选择一个特定的响应图像解决方案,包括这两个:如何避免重复下载响应图像中选择响应图像解决方案. 大家都在谈论的的sr ...