当你定义了一系列的变量时,需要写很多的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. jQuery - 左右拖动分隔条

    1.实现效果: 2.代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...

  2. 简单CSS3代码实现立方体以及3D骰子

    1 实现3D立方体 首先准备好UL以及6个Li: 代码如下 ul { position: absolute; top: 50%; left: 50%; transform:translate(-50% ...

  3. 认识 ARM、FPGA

    0. ARM ARM:Advanced RISC machine,微处理行业的一家知名企业.适用于多种领域,如嵌入控制,消费.教育类多媒体.DSP和移动式应用. 优势: 功耗低,不容易发热.死机: 3 ...

  4. 走在linux 的路上

    终于现在不看鸟哥的私房菜基础篇了,以后再慢慢看,像我这种初学者,感觉还是不太适合看鸟哥的私房菜. 于是从图书馆借了本书继续学习我的linux. 这样看着linux容易多了,进而熟悉了几个命令:ls c ...

  5. 事务的隔离级别以及oracle中的锁

    事务的概念及特性 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 例如:在关系数据库中,一个事务可以是一条SQL语句,一组SQL语 ...

  6. Linux的系统suspend和resume

    参考: www.wowotech.net/linux_kenrel/suspend_and_resume.htmlwww.wowotech.net/linux_kenrel/pm_interface. ...

  7. graphql-yoga 项目简单使用&&集成docker

      graphql-yoga 是一个实现了grahql的框架,使用简单,便捷 具体源码参考github https://github.com/rongfengliang/graphql-yoga-do ...

  8. RabbitMQ 概念与Java例子

    RabbitMQ简介 目前RabbitMQ是AMQP 0-9-1(高级消息队列协议)的一个实现,使用Erlang语言编写,利用了Erlang的分布式特性. 概念介绍: Broker:简单来说就是消息队 ...

  9. 【linux】ls常用参数

    1. 命令格式: ls [选项] [目录名] 2. 命令功能: 列出目标目录中所有的子目录和文件. 3. 常用参数: -a, –-all 列出目录下的所有文件,包括以 . 开头的隐含文件 -d, –- ...

  10. Java 权限框架 Shiro 实战一:理论基础

    Apache Shiro 官网地址:http://shiro.apache.org/ Apache Shiro is a powerful and easy-to-use Java security ...