ios的@property属性和@synthesize属性(转)
当你定义了一系列的变量时,需要写很多的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 的那种写法,所以变量名就不加前缀了。Y^o^Y
摘自:http://blog.csdn.net/chaoyuan899/article/details/10310719
ios的@property属性和@synthesize属性(转)的更多相关文章
- ios的@property属性和@synthesize属性
当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property 和@synthesize属性,@property用在 .h ...
- IOS中@property的属性weak、nonatomic、strong、readonly等介绍
iOS开发中@property的属性weak nonatomic strong readonly等介绍 //property:属性://synthesize:综合; @property与@synthe ...
- iOS 中@property() 括号中,可以填写的属性?
通过@property 和 @synthesize 属性可以简化设置器(set)和访问器(get) 的代码. 在@property 中又有那些属性呢? readwrite 默认 readonly 只读 ...
- (iOS)关于@property和@synthesize的理解(原创)
开始学习ios的时候,就对一些objc的语法不理解,就比如@property和@synthesize,之前都是记住然后照着用,但是写的代码多了,对objc和ios有了一些理解,再加上最近用MRC,所以 ...
- iOS对UIViewController生命周期和属性方法的解析
目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...
- 【iOS开发】iOS对UIViewController生命周期和属性方法的解析
iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...
- iOS UIView控件的常用属性和方法的总结
一 UIVIew 常见属性1.frame 位置和尺寸(以父控件的左上角为原点(0,0))2.center 中点 (以父控件的左上角为原点(0,0))3.bounds 位置和尺寸(以自己的左上角为原点 ...
- Android动画效果之Property Animation进阶(属性动画)
前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...
- 利用@property实现可控的属性操作
利用@property实现可控的属性操作 Python中没有访问控制符, 不像java之类的 public class Person{ private int x public int getAge( ...
随机推荐
- SQL Server 创建角色和账号
/*--示例说明 示例在数据库pubs中创建一个拥有表jobs的所有权限.拥有表titles的SELECT权限的角色r_test 随后创建了一个登录l_test,然后在数据库p ...
- New Concept English Two 32 88
$课文86 失控 940. As the man tried to swing the speedboat round, the steering wheel came away in his ha ...
- 使用c++实现一个FTP客户端(一)
之前使用c++实现了一个FTP客户端,在这里做一些记录. 一.需要注意的几点 ①FTP是一种文件传输协议,基于TCP,所以客户端与服务器建立的连接是可靠.安全的,并且要经过三次握手的过程. ②FTP传 ...
- eclipse+PyDev遇到字符UTF-8的问题
今天配置eclipse+PyDev,在配置的时候出现了问题,如下: python and jpython require at least version 2.1 and iron python 2. ...
- 来来来,有讲一个吐血的故事(matlab)之脚本运行路径是什么
脚本运行路径是什么,这真是太重要!! 重要1:你默认保存的路径 重要2:你访问的相对路径 先放图: 再看一幅图: 我的操作,点击左侧的文件夹,使上框的显示栏路径不一样,再点击运行,发现pwd指示的路径 ...
- 未能加载文件或程序集“NPOI”或它的某一个依赖项
自己遇到过得一个很麻瓜很耽误时间的bug,也请教了一些大神嫩是没找到解决方法 下面分享下问题和解决方法 做的是一个下载功能,本地是没问题IIS站点导出EXCEL的时候出错 我这边看不到错误信息,只能一 ...
- MacBook下配置android adb命令使用环境
想在Mac下使用android adb命令,常用的两种配置方式: 在MacBook下配置adb命令环境(方法一) 1.下载并安装IDE (android studio) 人性化的安装,直接点击下一步下 ...
- streamsets 包管理
streamsets 自带一个包管理,可以方便的进行三方组件的添加,比如我们需要处理mongodb 数据,默认是没有添加这个组件的,操作如下: 选择包管理 选择组件 安装 点击安装 提示界面 安装完成 ...
- ab压力测试之post与get请求
安装ab工具 yum install httpd-tools 参数说明 -n:执行的请求个数,默认时执行一个请求 -c:一次产生的请求个数,即并发个数 -p:模拟post请求,文件格式为gid=2&a ...
- ReportViewer 2010 打印预览,用鼠标快速切换显示比例时报错:存储空间不足,不能处理此命令
CreateCompatibleDIB 存储空间不足 无法处理此命令 安装 ReportViewer 2010 sp1 即可.