@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. win10 google浏览器设置

    在浏览器地址栏中输入命令: chrome://flags/ 撤销:chrome设置了禁止此页弹出提示框 chrome://settings/contentExceptions#popups

  2. spring3使用task:annotation-driven开始定时

    先看一个案例 package com.jCuckoo.demo; import java.text.SimpleDateFormat; import java.util.Date; import or ...

  3. Win7怎么用IIS发布网站系统 部署项目

      确保系统上已经安装IIS,如果没有安装 请到[控制面板]→[程序]→[程序和功能]→[打开或关闭Windows功能] 选中Internet 信息服务下面的所有选项,确定   获得发布好的程序文件 ...

  4. js学习笔记3---自定义属性

    1.自定义属性-----JS可以为任何 HTML元素 添加 任意个 自定义属性 方法:元素.属性 = 属性值 如:aBtn[0].abc = 123; 2.添加索引值,匹配数组 for(i=0; i& ...

  5. Xcode找不到模拟器出现"My Mac"

    问题如图: 步骤一. 找到target->built settings->Architectures->Base SDK, 选择你需要的版本;如果还是不行,看步骤二. 步骤二. 1) ...

  6. 【BZOJ-3174】拯救小矮人 贪心 + DP

    3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 686  Solved: 357[Submit][Status ...

  7. 【bzoj2104】 K-th Number

    http://poj.org/problem?id=2104 (题目链接) 题意 求区间第k大数. Solution1 主席树裸题. 主席树当时我学是学的要死,那个时候不晓得百度出什么bug了,搜个主 ...

  8. IIS如何设置可以让.aspx后缀的文件直接下载

    修改配置文件:<system.webServer> <staticContent> <remove fileExtension=".aspx" /&g ...

  9. webbench详解

    安装 mkdir -p /usr/local/man/man1 yum install ctags -y tar zxvf webbench-1.5.tar.gzcd webbench-1.5make ...

  10. C#的imagebutton

    常常要用到好看一点的button去做,C#本身不提供imagebutton这个控件. 可以用如下方法: 用imagebox添加image图片.对此image进行事件的处理. 有时候所添加的image过 ...