@property括号内属性讲解
#import <Foundation/Foundation.h> @interface Car : NSObject @property BOOL running;
@end Car.m #import "Car.h" @implementation Car @synthesize running = _running; //Xcode 4.4以上可选
@end
-(BOOL)running
{
return _running;
}
-(void)setRunning:(BOOL)running
{
_running = running;
}
当用
Car *honda = [[Car alloc] init];
honda.running = YES;
NSLog(@"%d",honda.running);
@property(getter=isRunning) BOOL running;
Car *honda = [[Car alloc] init];
honda.running = YES;
NSLog(@"%d",honda.running);
NSLog(@"%d",[honda isRunning]);
#import <Foundation/Foundation.h>
@interface Car : NSObject @property(getter=isRunning,readonly) BOOL running; -(void)startEngine;
-(void)stopEngine;
@end
-(void)startEngine
{
_running = YES;
}
-(void)stopEngine
{
_running = NO;
}
Car *honda = [[Car alloc] init];
// honda.running = YES;
NSLog(@"%d",honda.running);
honda.running = NO;
@property (nonatomic) NSString *model; //设置非原子性
@interface Person : NSObject @property(nonatomic)NSString *name;
@end
#import "Person.h" @implementation Person -(NSString *)description
{
return self.name;
}
@end
#import <Foundation/Foundation.h>
#import "Person.h" @interface Car : NSObject @property (nonatomic) NSString *model;
@property(nonatomic,strong) Person *driver; @end
Person *john = [[Person alloc] init];
john.name = @"John"; Car *honda = [[Car alloc] init];
honda.model = @"Honda Civic";
honda.driver = john;
NSLog(@"%@ is driving the %@",honda.driver,honda.model);
#import <Foundation/Foundation.h> @class Car; @interface Person : NSObject @property(nonatomic)NSString *name;
@property(nonatomic,strong)Car *car; @end
Person *john = [[Person alloc] init];
john.name = @"John";
Car *honda = [[Car alloc] init];
honda.model = @"Honda Civic";
honda.driver = john;
john.car = honda; //添加这行
NSLog(@"%@ is driving the %@",honda.driver,honda.model);
@property(nonatomic,weak)Car *car;
7、copy属性
Car *honda = [[Car alloc] init];
honda.model = @"Honda Civic"; NSMutableString *model = [NSMutableString stringWithString:@"Honda Civic"];
honda.model = model; NSLog(@"%@",honda.model);
[model setString:@"Nissa Versa"];
NSLog(@"%@",honda.model); //输出结果还是Honda Civic
- retain属性
- unsafe_unretained属性
- assign属性
- getter= 让getter方法使用自定义的名字
- setter = 让setter方法使用自定义名字
- readonly 不合成setter方法
- nonatomic 不保证在多线程环境下存取访问器的完整性,这比原子性更加高效
- strong 在属性和指定value之间创建一个拥有关系,这是默认的对象属性
- weak 在属性和制定value之间创建一个不拥有关系,使用这个防止循环引用
- copy 创建一个指定值的副本,而不是引用存在的对象实例。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,copy)NSArray *constArr;
@property (nonatomic,strong)NSMutableArray *mArr;
@property (nonatomic,strong)NSString *name;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]; _mArr = [[NSMutableArray alloc] initWithObjects:@"",@"", nil];
_constArr = _mArr;
NSLog(@"%@",self.constArr);
[_mArr addObject:@""];
NSLog(@"%@",self.constArr);
}
@end
这个例子中,虽然我们对constArr使用的是copy,但是两次打印的结果是不一样的。如果我们这样写:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,copy)NSArray *constArr;
@property (nonatomic,strong)NSMutableArray *mArr;
@property (nonatomic,strong)NSString *name;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]; _mArr = [[NSMutableArray alloc] initWithObjects:@"",@"", nil];
self.constArr = _mArr;
NSLog(@"%@",self.constArr);
[_mArr addObject:@""];
NSLog(@"%@",self.constArr);
}
@end
输出打印的结果才会是一样的,因为前者只是对实例变量的指针赋值,而后者才是利用了属性特性,对属性进行赋值。这也正解释了我们平时所理解的:
对于strong,为这种属性设置新值得时候,设置方法会先保留新值,并释放旧值,然后将新值设置上去。
对于copy,设置方法并不保留新值,而是将其拷贝。
上面提到的设置方法就是我们说的setter,也就是后者使用self.constArr调用的方法。
@property括号内属性讲解的更多相关文章
- iOS 中@property() 括号中,可以填写的属性?
通过@property 和 @synthesize 属性可以简化设置器(set)和访问器(get) 的代码. 在@property 中又有那些属性呢? readwrite 默认 readonly 只读 ...
- 第7.24节 Python案例详解:使用property函数定义属性简化属性访问代码实现
第7.24节 Python案例详解:使用property函数定义属性简化属性访问代码实现 一. 案例说明 本节将通过一个案例介绍怎么使用property定义快捷的属性访问.案例中使用Rectan ...
- 第7.23节 Python使用property函数定义属性简化属性访问的代码实现
第7.23节 Python使用property函数定义属性简化属性访问的代码实现 一. 背景 在本章前面章节中,我们介绍了类相关的知识,并举例进行了说明,在这些例子中会定义一些形如 ...
- Android动画效果之初识Property Animation(属性动画)
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
- OC中property的有关属性
property的有关属性: (1)readwrite是可读可写特征:需要生成getter方法和setter方法: (2)readonly是只读特性只会生成getter方法不会生成setter方法: ...
- windowSoftInputMode属性讲解
windowSoftInputMode属性讲解(下面这段内容我参考别人的博客,并加入我的一些意见) 我们从这个属性的名称中,可以很直观的看出它的作用,这个属性就是来设置窗口软键盘的交互模式的.andr ...
- 关于@property()的那些属性及ARC简介
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
- 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
- IOS UITableView NSIndexPath属性讲解
IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...
随机推荐
- TCP/IP 笔记 - Internet协议
IP是TCP/IP协议族中的核心协议,TCP.UDP.ICMP.IGMP数据都通过IP数据报传输.IP提供了一种"尽力而为.无连接"的数据交付服务:尽力而为表示不保证IP数据报能成 ...
- Spring Boot初识(3)- Spring Boot整合Swagger
一.本文介绍 如果Web项目是完全前后端分离的话(我认为现在完全前后端分离已经是趋势了)一般前端和后端交互都是通过接口的,对接口入参和出参描述的文档就是Mock文档.随着接口数量的增多和参数的个数增加 ...
- Python 3 进阶 —— print 打印和输出
在 Python 中,print 可以打印所有变量数据,包括自定义类型. 在 2.x 版本中,print 是个语句,但在 3.x 中却是个内置函数,并且拥有更丰富的功能. 参数选项 可以用 help( ...
- About me & 一些置顶的博文
About me 一只历史上最弱的 \(\text{hnoier}\) ... 身在 \(\text{hn}\) 弱校,除了在四大名校夹缝中生存,还要受到同校 \(\text{Julao}\) 的鄙视 ...
- [CF438E] 小朋友和二叉树
Description 给定一个整数集合 \(c\),对于每个 \(i\in[1,m]\),求有多少种不同的带点权的二叉树使得这棵树点权和为 \(i\) 并且顶点的点权全部在集合 \(c\) 中.\( ...
- [转]Angular2: Cannot read property 'name' of undefined
本文转自:https://stackoverflow.com/questions/39755336/angular2-cannot-read-property-name-of-undefined 处理 ...
- 简单JAVA爬虫51Jobs
使用Jsoup工具,它是一个HTML解析器,可以直接直接解析某个地址或者HTML文件.还可 通过Dom,CSS以及类似JQuery的操作方法操作数据. Jsoup官方文档地址:https://jsou ...
- lucene 核心概念及入门
lucene Lucene介绍及核心概念 什么是Lucene Lucene是一套用于全文检索和搜索的开放源代码程序库,由Apache软件基金会支持和提供.Lucene提供了一个简单却强大的应用程序接口 ...
- elasticsearch6.7 01.入门指南(2)
2.安装(略) 默认情况下,elasticsearch 使用端口 9200 来访问它的 REST API.如果有必要,该端口也可以配置 3.探索集群 3.1 The REST API 既然我们已经启动 ...
- 【读书笔记】iOS-自定义URL Scheme注意事项
如果两个不同的应用注册了同样的URL Scheme,那么后安装的应用会响应符合这种协议格式的URL. 如果你的应用的iPhone和iPad版是各自独立的(即不是Universal类型的),那么你就不应 ...