@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 ...
随机推荐
- Ribbon使用Hystrix
1.导入依赖spring-cloud-starter-hystrix <dependency> <groupId>org.springframework.cloud</g ...
- MongoDB-Oplog详解
MongoDB Oplog 详解 Oplog 概念 Oplog 是用于存储 MongoDB 数据库所有数据的操作记录的(实际只记录增删改和一些系统命令操作,查是不会记录的),有点类似于 mysql 的 ...
- leetcode — reverse-integer
/** * Source : https://oj.leetcode.com/problems/reverse-integer/ * * Created by lverpeng on 2017/7/4 ...
- MYSQL中的COLLATE是什么?
本文由horstxu发表 在mysql中执行show create table <tablename>指令,可以看到一张表的建表语句,example如下: CREATE TABLE `ta ...
- vi常用命令总结
1. 打开文件 > vi 文件 //该模式是命令模式 2. 尾行模式操作 > :q //该模式是“尾行模式” > :w //保存已经修改的文档 > :wq //保存并退出 &g ...
- Sphinx coreseek 3.2
功能 中文的拆词索引 MySQL中like模糊查询. >1>5>%可以用索引 配置 用编辑器打开 path 配置 改绝对路径 https://blog.csdn.net/u013 ...
- Oracle11g em启动报此网站的安全证书有问题的解决方案
http://blog.sina.com.cn/s/blog_a32eff280101cgje.html C:\>emctl status dbconsoleOracle Enterprise ...
- 【Redis】3、Redis集群部署
Redis 集群是一个提供在多个Redis间节点间共享数据的程序集. Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下 ...
- 第一篇 Spring boot 配置文件笔记
spring boot 不需要配置太多文件程序便可正常运行,特殊情况需要我们自己配置文件. 项目以IDEA写实例,系统会默认在src/main/java/resources目录下创建applicati ...
- Shaping Regions(dfs)
Shaping Regions Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 124 Solved: 39[Submit][Status][Web B ...