@property

1,在@interface中

2,自动生成setter和getter的声明

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
int _age;
// int age; int _height; double _weight; NSString *_name;
} // @property:可以 自动生成某个成员变量的setter和getter声明
@property int age;
//- (void)setAge:(int)age;
//- (int)age; @property int height;
//- (void)setHeight:(int)height;
//- (int)height; - (void)test; @property double weight; @property NSString *name; @end

Person.m

#import "Person.h"

@implementation Person

// @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量
@synthesize age = _age; @synthesize height = _height; @synthesize weight = _weight, name = _name; @end
#import "Person.h"

@implementation Person

// @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量
@synthesize age = _age; @synthesize height = _height; @synthesize weight = _weight, name = _name; @end

@property关键字

        自动生成某个成员变量的setter和getter方法的声明

        相当于 - (void)setAge:(int)age;

            - (int)age;

@synthesize

语法: @synthesize age = _age;

相当于:

    - (void) setAge:(int)age

    {  

       _age = age;

    }

  - (int)age

    {

        return _age;

    }

如果成员变量_age不存在,就会自动生成一个@private的成员变量_age

若:@synthesize age;

  setter和getter实现中会访问成员变量age

  如果成员变量age不存在,就会自动生成一个@private的成员变量age

手动实现:

  若手动实现了setter方法,编译器就只会自动生成getter方法

  若手动实现了getter方法,编译器就只会自动生成setter方法

  若同时手动实现了setter和getter方法,编译器就不会自动生成不存在的成员变量

新特性:

  自从Xcode 4.x后, @property就独揽了@synthesize的功能

  默认情况下,setter和getter方法中的实现,会去访问下划线_开头的成员变量

OC- @property @synthesize的更多相关文章

  1. OC @property @synthesize和id

    文顶顶   OC语言@property @synthesize和id OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字 ...

  2. OC语言@property @synthesize和id

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

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

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

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

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

  5. 五.OC基础--1.多态,2.类对象,3.点语法,4.@property&@synthesize,5.动态类型,内省(判断对象是否遵循特定的协议,以及是否可以响应特定的消息)

    五.OC基础--1.多态, 1. 多态概念,定义:多态就是某一类事物的多种形态: 表现形式: Animal *ani = [Dog new]; 多态条件:1.有继承关系 2.有方法的重写 2.多态代码 ...

  6. 「OC」@property @synthesize和id

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

  7. iOS知识基础篇--@property,@synthesize, nonatomic,atomic,strong,weak,copy,assign,retain详解

    一.@property 这个关键词的唯一作用就是声明getter.setter方法接口. 二.@synthesize 实现setter.getter方法,找不到实例变量则主动创建一个. 三.nonat ...

  8. @property & @synthesize & @dynamic 及相关属性作用探究

    @property : iOS6 引入关键词. @property name; 指示编译器自动生成 name 的 setter 和 getter 方法 : - (NSString *)name; - ...

  9. @property @synthesize的含义以及误区。

    @property的作用是定义属性,声明getter,setter方法.(注意:属性不是变量) @synthesize的作用是实现属性的,如getter,setter方法. 在声明属性的情况下如果重写 ...

  10. ios中的关键词@property @synthesize

    @interface Person : NSObject{    int myNumber;} @property(nonatomic) int myNumber;//这个关键字是可以带套get 与s ...

随机推荐

  1. Jquery ui autocomplete简单api

    重要说明:与配置选项类似,Autocomplete插件的方法也不是直接调用,而且通过autocomplete()方法进行间接调用.例如: $("#title").autocompl ...

  2. 转JAVA2

    1.List遍历时删除的几种方式比较 1.1.会报错的删除方式: (1)在Iterator遍历时使用list删除 Iterator<String> it = list.iterator() ...

  3. Linux基础3(用户/组管理,rpm,yum,源码安装软件)

    用户管理 与用户相关的配置文件 /etc/passwd /etc/shadow /etc/skel /etc/defalut/useradd /etc/login.defs useradd userm ...

  4. JAVA基本类型的转换

    1.String转成Int 例1: String str = "123"; try { int a = Integer.parseInt(str); } catch (Number ...

  5. [NOIP2014] 提高组 洛谷P2038 无线网络发射器选址

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...

  6. Allegro笔记三

    1.设置Gerber导出目录 可以在$Install_Dir/share/pcb/text/env.txt目录里面添加:“set artpath = . ../Gerber/”语句. 其他各种文件夹设 ...

  7. hihocoder 1356 分隔相同整数

    时间限制:10000ms单点时限:1000ms内存限制:256MB 描述 给定一个包含N个整数的数组A.你的任务是将A重新排列,使得任意两个相等的整数在数组中都不相邻. 如果存在多个重排后的数组满足条 ...

  8. AngularJs Cookies 操作

    $cookiesProvider 使用$cookiesProvider改变$cookies服务的默认行为. 默认属性 path:字符串,cookies只在这个路径及其子路径可用.默认情况下,这个将会是 ...

  9. CF 363B One Bomb(枚举)

    题目链接: 传送门 One Bomb time limit per test:1 second     memory limit per test:256 megabytes Description ...

  10. Nginx系列1之部分模块详解

    1 内核模块: 名称: daemon 语法: daemon on |off 默认值: on 功能: 决定nginx 在前台执行还是后台守护进程执行的 ================== 名称: En ...