1、@property int age;
在编译器情况下会自动编译展开为:
<age在setter中首字母大写,点语法为p.age>
- (void)setAge:(int)age;
- (int)age;
 
2、同理:@property int _age;
在编译器情况下会自动编译展开为:
<_age在setter中首字母大写,为横线,大写仍保持,此时点语法为p._age>
- (void)set_age:(int)age;
- (int)_age;
一般情况,使用1,而不使用2.

@synthesize age = _age;
会访问_age这个成员变量,如果_age成员变量不存在,系统就会自动生成@private类型的_age的成员变量,并自动生成age的setter与getter

在4.5版本之后,可以省略@synthesize,此时,
@property int age;有三个作用
1、生成setter与getter的声明
2、生成_age的成员变量
3、生成setter与getter的实现

//注意:必须在非ARC下,在“项目——》building setting——》all——》objcet-C Automic Reference Counting
//@property(nonatomic,retain) NSString *name相当于:
- (void)setName:(NSString *)name
{
    if (_name != name)
    {
        [_name release];
        _name = [name retain
];
    }
}
否则,无法操作 [_name release];
        _name = [name retain
];


// @property:可以自动生成某个成员变量的setter和getter声明
@property int age;
//- (void)setAge:(int)age;
//- (int)age;

@property int height;
//- (void)setHeight:(int)height;
//- (int)height;


可以简写为:@property int age, height;
 
注意:生成的是age、height而不是_age、_height成员变量
 
 
语义设置(assign、retain、copy) 
基本数据类型用assign;NSString用copy、(retain也可以使用);非NSString对象用过retain
 
例:
@property(nonatomic,assign)int age;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,retain)NSString *number;
@property(nonatomic,retain)NSArray *group;

@synthesize age = _age;
// @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量,4.4后自动生成。
 
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
    //int _speed;
    //int _wheels;
}
@property int speed;
@property int wheels;

//@property int speed, wheels;
- (void)test;
@end

 
#import "Car.h"

@implementation Car
//@synthesize speed = _speed, wheels = _wheels;
// 注意:会访问_speed这个成员变量,如果不存在,就会自动生成@private类型的_speed变量
@synthesize speed = _speed;
@synthesize wheels = _wheels;

- (void)test
{
    NSLog(@"速度=%d", _speed);
}

@end

 
同理:
@synthesize age;
// 默认会访问age这个成员变量,如果没有age,就会自动生成@private类型的age变量

@property int age;
如果.m中有setter或getter,系统会生成成员变量以及getter或setter。若既存在setter又存在getter,则不再会产生getter、setter以及成员变量,因为成员变量是为setter与getter服务的。此时(三无),@synthesize age = _age;无法省略。

id等价于NSObject*,万能指针,能指向或操作任何OC对象

property、synthesize、id的更多相关文章

  1. IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic、 @synthesize、@property、@dynamic

    IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic.                     @synth ...

  2. 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方 ...

  3. Objective-C 点语法 成员变量的作用域 @property和@synthesize关键字 id类型

    点语法 1.利用点语法替换set方法和get方法 方法调用 Student *stu = [Student new]; [stu setAge : 18]; int age = [stu age]; ...

  4. OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法

    一. 分类-Category 1. 基本用途:Category  分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...

  5. @property、@synthesize和dynamic的用法

    原文:  http://blog.csdn.net/hherima/article/details/8622948 @代表“Objective-C”的标志,证明您正在使用Objective-C语言 O ...

  6. ios中点语法、property跟synthesize用法

    一:OC中得点语法 1> 点语法的基本使用: ·使用 对象.成员变量   可以实现设置成员变量值,和获取成员变量的值   2> 点语法的本质 (点语法是Xcode编译器自己帮我们完成的一个 ...

  7. OC语言@property @synthesize和id

    OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...

  8. 李洪强iOS开发之OC语言@property @synthesize和id

    OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...

  9. 「OC」@property @synthesize和id

    一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...

随机推荐

  1. HDU 3030 - Increasing Speed Limits

    Problem Description You were driving along a highway when you got caught by the road police for spee ...

  2. git工作区和暂存区

    工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区: 版本库(Repository) 工作区有一个隐藏目录.git,这个不算工作区, ...

  3. php学习网址

    后面会陆续维护此页. 1. php编程 此博客是网站www.beilei123.cn镜像,转载请注明出处.

  4. 做了一个jquery插件,使表格的标题列可左右拉伸

    示例下载 插件名称命名为:jquery.tableresize.js,代码如下: /* Writen by mlcactus, 2014-11-24 这是我封装的一个jquery插件,能够使table ...

  5. php 数据结构 hash表

    hash表 定义 hash表定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值进行数据库搜索更快,这种方法一般用来 ...

  6. Codeforces 286E

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  7. cdoj Dividing Numbers 乱搞记忆化搜索

    //真tm是乱搞 但是(乱搞的)思想很重要 解:大概就是记忆化搜索,但是原数据范围太大,不可能记下所有的情况的答案,于是我们就在记下小范围内的答案,当dfs落入这个记忆范围后,就不进一步搜索,直接返回 ...

  8. UI线程与worker线程

    也谈谈我对UI线程和worker线程的理解 UI线程又叫界面线程,能够响应操作系统的特定消息,包括界面消息.鼠标键盘消息.自定义消息等,是在普通的worker线程基础上加上消息循环来实现的,在这个消息 ...

  9. Spring、实例化Bean的三种方法

    1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...

  10. 用Meta 取消流量器缓存实现每次访问都刷新页面方便调试

    如果想禁止浏览器从本地缓存中调阅页面,可以设置网页不保存在缓存中,每次访问都刷新页面,下面是Meta在这方便的用法,需要的朋友可以参考下: <!-- 禁止浏览器从本地缓存中调阅页面.--> ...