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

如下,新建一个基于“Command Line Tool”的项目,名为“property”,再新建一个Student类,传统的写法是:

Student.h

 
//  Student.h
// property
//
// Created by Rio.King on 13-8-25.
// Copyright (c) 2013年 Rio.King. All rights reserved.
// #import <Foundation/Foundation.h> @interface Student : NSObject
{
int age;
int no;
} //age的getter和setter方法声明
- (int)age;
- (void)setAge:(int)newAge; //no的getter和setter方法声明
- (int)no;
- (void)setNo:(int)newNo; @end
 

Student.m

 
//
// Student.m
// property
//
// Created by Rio.King on 13-8-25.
// Copyright (c) 2013年 Rio.King. All rights reserved.
// #import "Student.h" @implementation Student //age的getter和setter方法的实现
- (int)age
{
return age;
}
-(void)setAge:(int)newAge
{
age = newAge;
} //no的getter和setter方法的实现
- (int)no
{
return no;
}
- (void)setNo:(int)newNo
{
no = newNo;
} @end
 

main.m

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

************************************************************************************

用@property和@synthesize的写法是:
Student.h

 
//
// Student.h
// property
//
// Created by Rio.King on 13-8-25.
// Copyright (c) 2013年 Rio.King. All rights reserved.
// #import <Foundation/Foundation.h> @interface Student : NSObject
{
int age;
int no;
} //当编译器遇到@property时,会自动展开成getter和setter的声明
@property int age;
@property int no; @end
 

Student.m

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

main.m

 
//
// main.m
// property
//
// Created by Rio.King on 13-8-25.
// Copyright (c) 2013年 Rio.King. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Student.h" int main(int argc, const char * argv[])
{ @autoreleasepool { // insert code here...
Student *stu = [[Student alloc] init];
stu.age = 100;
NSLog(@"age is %i", stu.age); [stu release];
}
return 0;
}
 

几点说明:
1.在Xcode4.5及以后的版本中,可以省略@synthesize ,编译器会自动帮你加上getter 和 setter 方法的实现,并且默认会去访问_age这个成员变量,如果找不到_age这个成员变量,会自动生成一个叫做 _age的私有成员变量。

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

语法简介:

格式: 声明property的语法为:@property (参数1,参数2) 类型 名字; 如:
C代码

@property(nonatomic,retain) UIWindow *window;

其中参数主要分为三类: 
读写属性: (readwrite/readonly)
setter语意:(assign/retain/copy)
原子性: (atomicity/nonatomic)

各参数意义如下: 
readwrite: 产生setter\getter方法
readonly: 只产生简单的getter,没有setter。
assign: 默认类型,setter方法直接赋值,而不进行retain操作
retain: setter方法对参数进行release旧值,再retain新值。
copy: setter方法进行Copy操作,与retain一样
nonatomic: 禁止多线程,变量保护,提高性能
(详见:http://justcoding.iteye.com/blog/1444548

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. 汇编语言中,SP,BP ,SI,DI作用?

    这个很简单: sp:表示栈顶指针,指向栈顶地址.与SS相配合使用.ss为栈段. bp:是基址指针,段地址默认在SS中.可以定位物理地址,比如:"mov ax,[bp+si+6]/mov ax ...

  2. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  3. 微小说《tree》

    自己一直是一个矛盾纠结的人,计算机专业,却喜欢画画,偶尔会写些短文,寓言,或者酸酸的情诗.. 废话不多说,各位看官有兴趣便随意看看,若有些于共鸣,便共勉之. 正文 两株幼苗从土壤中破土而出,一颗天生强 ...

  4. Keepalived+MySQL双主

    一.Keepalived+MySQL Replication的应用场景 MySQL的高可用方案有cluster,MMM,MHA等,这些高可用方案都要三台服务器以上,成本有点高,今天介绍一个低成本高可用 ...

  5. 在Windows中隐藏用户的方法

    这两天新建了一个用户用于共享文件,由于我的电脑只有我一个人用,多了一个用户后在登录界面上觉得挺碍事的,便想把它隐藏起来.找了一下,可以通过如下方式实现: 在注册表编辑器新建项值: HKEY_LOCAL ...

  6. Win32下 Qt与Lua交互使用(四):在Lua脚本中自由执行Qt类中的函数

    话接上篇.通过前几篇博客,我们实现在Lua脚本中执行Qt类中函数的方法,以及在Lua脚本中连接Qt对象的信号与槽. 但是,我们也能发现,如果希望在Lua脚本中执行Qt类的函数,就必须绑定一个真正实现功 ...

  7. 使用PowerShell脚本部署定时器到MOSS2010

    转:http://www.77site.com/tech/1087042010072906074113_2012050808152911.html 第一章 前言 在此次练习中,您将了解到如何使用Pow ...

  8. c#等待所有子线程执行完毕方法

    当我们在使用线程中,你会发现主线结束后子线程的结果才显示出来.现在我要等待所以子线程结束,然后在显示结果,怎么做呢? 方法如下: 1.使用 ManualResetEvent,代码如下:  using  ...

  9. 转载-smarty教程(基本语法)

    转自:http://hi.baidu.com/qxxgvpdtzhbckpr/item/681049160d7be60db98a1aec 1.smarty的配置      首先,使用smarty第一件 ...

  10. [转]oracle性能调优之--Oracle 10g AWR 配置

    一.ASH和AWR的故事 1.1 关于ASH 我们都知道,用户在ORACLE数据库中执行操作时,必然要创建相应的连接和会话,其中,所有当前的会话信息都保存在动态性能视图V$SESSION中,通过该视图 ...