当你定义了一系列的变量时,需要写很多的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. java.io.File中的 pathSeparator 与separator 的区别

    先总的说一下区别: File.pathSeparator指的是分隔连续多个路径字符串的分隔符,例如: java   -cp   test.jar;abc.jar   HelloWorld 就是指“;” ...

  2. WebGL编程指南案例解析之绘制三角形

    //案例3.绘制三角形,将顶点数据存到缓冲区对象(gl.ARRAY_BUFFER)中,然后顶点着色器从里面读数据(3个顶点) //顶点着色器中去掉gl_PointSize = 10.0,绘制三角不能设 ...

  3. Java第七次作业--图形用户界面

    Deadline: 2017-5-11 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 了解GUI开发的相关原理和技巧 熟悉Swint组件的使用 理解事件处理模型 二.作业要求 发布 ...

  4. L3-009 长城 (30 分)

    正如我们所知,中国古代长城的建造是为了抵御外敌入侵.在长城上,建造了许多烽火台.每个烽火台都监视着一个特定的地区范围.一旦某个地区有外敌入侵,值守在对应烽火台上的士兵就会将敌情通报给周围的烽火台,并迅 ...

  5. python 生成器 迭代器 yiled

    文章来源:http://python.jobbole.com/81911/ https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449 ...

  6. 报表生成poi----java操作java对象生成execl表单

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  7. ballerina 学习二十五 项目docker 部署&& 运行

    ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...

  8. UNIX 时间戳 C#

    /// 将Unix时间戳转换为DateTime类型时间 /// </summary> /// <param name="d">double 型数字</ ...

  9. 949. Largest Time for Given Digits

    Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour t ...

  10. 【转】vim环境设置和自动对齐

    原文网址:http://blog.chinaunix.net/uid-23525659-id-4340245.html 注:如果是用vim编写代码,建议开启vim的文件类型自动检测功能,这样编写代码换 ...