Xcode编译器的特性,自动生成getter和setter
 

A.@property

自动生成某个成员变量的getter和setter的声明
变量的命名要求:以下划线开头
 Student.h
@interface Student : NSObject
{
int _age;
int _no;
int age;//此变量不会被访问到
double height;
NSString *_name;
} @property int age, no;
@property int height;
@property NSString *name; @end
 
B.@synthersize
自动生成getter和setter的实现
 @implementation Student

 @synthesize age = _age, no = _no, height = _height;//指定一下成员变量,否则会访问同名变量,如果不存在,就会自动生成@private的变量
@synthesize name = _name; @end
C.省略变量的声明,在.h中得声明可以省略
注意:XCode自动生成的成员变量都是@private
 
 @interface Student : NSObject

 @property int age;
@property double height;
@property NSString *name; @end

D.省略实现(XCode4.4以上)
只需要写@property,默认访问下划线开头的成员变量
 @interface Car : NSObject

 @property int wheels;

 @end
 
E.如果手动实现了setter和getter,编译器就不再自动生成下划线开头的成员变量
 

[Objective-c 基础 - 2.6] @property和@synthesize的更多相关文章

  1. Objective-C基础笔记(2)@property和@synthesize

    先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个keyword的使用方法和含义,代码例如以下: Person.h文件 #import <Foundation ...

  2. @property和@synthesize的特性

    基础回顾:get方法和set方法 定义类成员变量时,可以在@interface中定义,也可以在@implementation中定义: 在@interface中声明,成员变量的状态是受保护的,即“@pr ...

  3. iOS 内存管理-copy、 retain、 assign 、readonly 、 readwrite、nonatomic、@property、@synthesize、@dynamic、IB_DESIGNABLE 、 IBInspectable、IBOutletCollection

    浅谈iOS内存管理机制 alloc,retain,copy,release,autorelease 1)使用@property配合@synthesize可以让编译器自动实现getter/setter方 ...

  4. @synthesize obj=_obj的意义详解 @property和@synthesize

    本文转载至 http://blog.csdn.net/ztp800201/article/details/9231969 http://hi.baidu.com/feng20068123/item/c ...

  5. Objective-C中的@property和@synthesize用法

    @代表“Objective-C”的标志,证明您正在使用Objective-C语言 Objective-C语言关键词,@property与@synthesize配对使用. 功能:让编译好器自动编写一个与 ...

  6. iOS 详细解释@property和@synthesize关键字

    /** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...

  7. OC中两个关键字的作用:@property和@synthesize

    两个关键字的使用:@property和@synthesize 一.@property关键字这个关键字是OC中能够快速的定义一个属性的方式,而且他可以设置一些值,就可以达到一定的效果,比如引用计数的问题 ...

  8. OC语法5——@property和@synthesize

    @property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...

  9. Objective-c @property和@Synthesize

    在Objective-c中,使用@property来标识属性(一般是实例变量).在实现文件中使用@synthesize标识所声明的变量,让系统自动生成设置方法和获取方法. 也就是说@property和 ...

随机推荐

  1. redis 参考

    http://redis.readthedocs.org/en/2.4/index.html

  2. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  3. 概率图模型之有向图与无向图之间的关系 I map D map perfect map(完美图) 概念

    我们已经讨论了有向图和无向图框架下的概率模型,那么我们有必要讨论一下它们二者的关系.

  4. unite

    列出某个集合里的项目,比如file,buffer等 :United file——列出文件 :United buffer——列出buffer :United file_rec——递归列出文件 进入Uni ...

  5. 用PHP尝试RabbitMQ(amqp扩展)实现消息的发送和接收

    消费者:接收消息 逻辑:创建连接-->创建channel-->创建交换机-->创建队列-->绑定交换机/队列/路由键-->接收消息 <?php /********* ...

  6. FastScroll(2)不分组的listview 打开fastscroll的分组提示功能

    本文只让fastscroll具有提示分组功能,但listview并不显示分组,如果想让分组的listview显示fastscroll,看下篇. 1,在listview中打开fastscroll 2,自 ...

  7. HTML5学习(七)----地理定位

    参考教程:http://www.w3school.com.cn/html5/html_5_geolocation.asp 说明:设备必须有GPS定位功能才能定位的 定位用户的位置 HTML5 Geol ...

  8. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  9. JPA简单知识

    ,JPA(Java Persistence API):通过注解或XML描述对象--关系表的映射关系,并将运行期的实体对象持久化到数据库中. JPA是一套规范,不是某个ORM产品,它主要包括以下3方面的 ...

  10. ViewPager onPageChangeListener总结

    今天在做项目的时候,由于要处理viewPager页面滑动的事件,所以对其进行了一个小小的研究: 首先ViewPager在处理滑动事件的时候要用到OnPageChangeListener OnPageC ...