OC- @property @synthesize
@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的更多相关文章
- OC @property @synthesize和id
文顶顶 OC语言@property @synthesize和id OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字 ...
- OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- 李洪强iOS开发之OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
一. 分类-Category 1. 基本用途:Category 分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...
- 五.OC基础--1.多态,2.类对象,3.点语法,4.@property&@synthesize,5.动态类型,内省(判断对象是否遵循特定的协议,以及是否可以响应特定的消息)
五.OC基础--1.多态, 1. 多态概念,定义:多态就是某一类事物的多种形态: 表现形式: Animal *ani = [Dog new]; 多态条件:1.有继承关系 2.有方法的重写 2.多态代码 ...
- 「OC」@property @synthesize和id
一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...
- iOS知识基础篇--@property,@synthesize, nonatomic,atomic,strong,weak,copy,assign,retain详解
一.@property 这个关键词的唯一作用就是声明getter.setter方法接口. 二.@synthesize 实现setter.getter方法,找不到实例变量则主动创建一个. 三.nonat ...
- @property & @synthesize & @dynamic 及相关属性作用探究
@property : iOS6 引入关键词. @property name; 指示编译器自动生成 name 的 setter 和 getter 方法 : - (NSString *)name; - ...
- @property @synthesize的含义以及误区。
@property的作用是定义属性,声明getter,setter方法.(注意:属性不是变量) @synthesize的作用是实现属性的,如getter,setter方法. 在声明属性的情况下如果重写 ...
- ios中的关键词@property @synthesize
@interface Person : NSObject{ int myNumber;} @property(nonatomic) int myNumber;//这个关键字是可以带套get 与s ...
随机推荐
- mysql命令-use
use命令可以让我们来使用数据库. use命令格式: use <数据库名>; 例如,如果xhkdb数据库存在,尝试存取它: mysql> use xhkdb; 屏幕提示:Databa ...
- retinajs 使用方法
本文根据retinajs的官网翻译,如果有翻译错的地方,还请朋友指正.谢谢. 工作原理: 现在有4种方式: 1.自动交换“img”标签的"src"路径. 2.在内联样式中自动交换背 ...
- shell 字符串截取
${expression}一共有9种使用方法. ${parameter:-word},如果parameter为空,则用word的值做parameter的缺省值 ${parameter:=word},在 ...
- const、static、extern三个关键字
默认情况下,C语言的全局变量是全世界都可以访问的,也就是全局变量可以跨文件访问. extern可以引用全局变量 例如,如果有一个全局变量int money = 100;extern int money ...
- perl split 的一种特殊用法
参考 http://blog.chinaunix.net/uid-1919528-id-2792055.html split 函数的正规语法应该是: split /PATTERN/, EXPR 而使用 ...
- 绘制图形与3D增强技巧(一)----点图元
1.图元 1.点图元 glBegin(GL_POINTS); glend(); 程序:点图元的应用 #include "stdafx.h" #include<stdio.h& ...
- hibernate查询返回一个list ,Date类型追加数据
public Pagination getLookPage(BeanPatrolScheduling beanPatrolScheduling, int pageNo, int pageSize) { ...
- bzoj 3518 Dirichlet卷积
详情见代码,回头再填坑... #include<iostream> #include<cstdio> #include<algorithm> #include< ...
- 【BZOJ-3786】星系探索 Splay + DFS序
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 647 Solved: 212[Submit][Status][Discuss] ...
- 【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 247 Solved: 113[Submit][Status][Discuss ...