当你定义了一系列的变量时,需要写很多的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. Terminal shortcuts

    <backspace> 删除 <ctrl+l> 清空屏幕, 相当于clear <ctrl+e> 光标跳至命令结尾 <ctrl+a> 光标跳至命令开始 & ...

  2. Visual Studio 后期生成事件命令行

    set "str=$(ConfigurationName)" if "%str%"=="Release" (xcopy /y/e $(Tar ...

  3. IE8下的typeof(console.log)为"object"的BUG

    今天发现IE8在开启过控制台后,console.log虽然可用,也是确实是一个函数,但是对其执行typeof操作返回的确是"object" 原生IE8:

  4. Linux wget auto login and backup database

    #!/bin/bash # 这是一份本来打算采用自动备份数据的代码,由于测试过程中出现了无法连接的问题,导致不能测试, # 于是最后放弃了这份代码的进一步的开发,但是记录还是有必要的 login_ur ...

  5. php 递归读取目录

    看到很多面试题有这个,今天有机会写了一下. 要注意的是: 在opendir这个函数用完后,要注意closedir,因为安全问题,打开的目录依然存在于内存中,在并发情况下最好关闭,不然容易被破坏. &l ...

  6. BZOJ4881: [Lydsy1705月赛]线段游戏(二分图)

    4881: [Lydsy1705月赛]线段游戏 Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 359  Solved: 205[Submit][Stat ...

  7. postgraphile 基本试用

    我的测试环境使用docker 进行的安装 基本安装 pg 数据库 我使用的是timesacledb 复制集版本,参考https://github.com/timescale/streaming-rep ...

  8. Javascript-自己定义对象转换成JSon后怎样再转换回自己定义对象

    man是自己定义的对象,使用var tim = JSON.stringify(man); var newman=JSON.parse(tim)后newman的类型是"object" ...

  9. Servlet技术基础

    由于Servlet部分涉及较多的类,要想尽快掌握Servlet基础,必须熟悉使用这些类之间的关系以及其常用的方法. 主要讲解部分包括: 1)通过实现Servelt接口来编写Servlet 2)熟悉Se ...

  10. java System.arrayCopy使用说明

    java System.arrayCopy使用说明 java.lang.System.arraycopy() 方法复制指定的源数组的数组,在指定的位置开始,到目标数组的指定位置. 下面是 System ...