@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. nodejs-helloword案例

    确认正确看着了node 本例处理环境win10 创建文件helloword.js.如下图:存放位置 具体内容详细 var http = require('http'); http.createServ ...

  2. json-jsonConfig使用

    一,setCycleDetectionStrategy 防止自包含 /** * 这里测试如果含有自包含的时候需要CycleDetectionStrategy */ public static void ...

  3. C#把datetime类型的日期转化成年月日或其他格式方法总结

    日期格式:yyyyMMdd HH:mm:ss(注意此字符串的字母大小写很严格) yyyy:代表年份MM:  代表月份dd:  代表天HH:  代表小时(24小时制)mm:  代表分钟ss:  代表秒D ...

  4. asp.net(C#)时间相减 得到天数、小时、分钟、秒差

    asp.net(C#)时间相减 得到天数.小时.分钟.秒差 DateTime dtone = Convert.ToDateTime("2007-1-1 05:00:00"); Da ...

  5. 从CIO、CEO、CFO、COO...到CVO 这22个你了解几个? (史上最完整版)

    1.CEO:是Chief Executive Officer的缩写,即首席执行官. 由于市场风云变幻,决策的速度和执行的力度比以往任何时候都更加重要.传统的“董事会决策.经理层执行”的公司体制已经难以 ...

  6. UIScrollView 实践经验(转)

    转载自:http://tech.glowing.com/cn/practice-in-uiscrollview/ UIScrollView(包括它的子类 UITableView 和 UICollect ...

  7. Core Foundation框架

    转载自:http://blog.csdn.net/weiwangchao_/article/details/7744972 Core Foundation框架 (CoreFoundation.fram ...

  8. hadoop-MapReduce分布式计算框架

    计算框架: MapReduce:主要用于离线计算 Storm:流式计算框架,更适合做实时计算 stack:内存计算框架,快速计算 MapReduce设计理念: --何为分布式计算 --移动计算,而不是 ...

  9. 谈谈JavaScript MVC模式

    第一个是:没有使用mvc模式的: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  10. LuaLaTeX \documemtclass{standalone} 编译错误

    最近在学习用LaTeX中的TikZ包画图 (graph), 必须要用LuaLaTeX编译. 然而却出现了奇怪的错误. \documentclass{standalone} \usepackage{ti ...