当你定义了一系列的变量时,需要写很多的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. 调试Android USB遇到的令人费解的问题

    上周参照网上代码,做了USB的初步探测程序,工作正常 .今天从硬件部拿到了一段例程,原本打算参考它来完善自己的程序.但运行之后总是报错,逐步跟进错误,进而发现了一个匪疑所思的问题.调试一天也未发现原因 ...

  2. 把硬盘格式化成ext格式的cpu占用率就下来了

    把硬盘格式化成ext格式的cpu占用率就下来了我是使用ext4格式 @Paulz 还有这种事情? 现在是什么格式?- - ,你自己用top命令看一下啊就知道什么东西在占用cpu了下载软件一半cpu都用 ...

  3. vi/vim 基本使用

    摘要: 在minicom终端里修改开发板中的文件时,必须要用到vi,因为开发板中并不像开发主机那样,有gedit和kscope这样的编辑器:还有,即便是在开发主机上,也会经常用到vi,因为vi使用起来 ...

  4. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  5. bzoj3796

    好像已经很久没有做后缀数组的题目,导致这种题一开始没想出来看到公共子串肯定想到后缀数组吧,但我都忘了最长公共子串怎么求了重要的性质:最长公共子串=max(h[i])名次相邻的两个后缀要分别属于s1,s ...

  6. 如何将磁盘从GPT格式转换成MBR

      GPT转MBR分区怎么转?现在很多笔记本的硬盘分区都是GPT模式,如果想装XP的话,那只能将GPT磁盘转换成MBR磁盘分区才行.接下来,简单说说如何将GPT分区转成 MBR分区! 如果本身电脑有两 ...

  7. C#微信公众号开发系列教程(接收事件推送与消息排重)

    微信服务器在5秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次.这样的话,问题就来了.有这样一个场景:当用户关注微信账号时,获取当前用户信息,然后将信息写到数据库中.类似于pc端网站的注册.可 ...

  8. Linux kernel ‘aac_send_raw_srb’函数输入验证漏洞

    漏洞名称: Linux kernel ‘aac_send_raw_srb’函数输入验证漏洞 CNNVD编号: CNNVD-201311-422 发布时间: 2013-11-29 更新时间: 2013- ...

  9. 关于app transfer之后的开发

    原文  http://blog.csdn.net/donghong2008/article/details/38020855 网络上有很多开发者提问怎么转让App并想知道具体的流程.实际上Appsto ...

  10. http://www.cnblogs.com/eye-like/p/4121219.html

    c# 操作Word总结 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中, ...