@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 ...
随机推荐
- svgalib_1.4.3 移植
运行环境 RedHat 6.3 Linux localhost 2.6.32-279.el6.i686 需准备好的文件: libx86_1.1+ds1.orig.tar.gz libx86_1.1+d ...
- MailBee.NET
MailBee.NET Objects 是一款为创建.发送.接收以及处理电子邮件而设计的健壮.功能丰富的.NET控件.具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加 ...
- osgi.net框架简介
osgi.net是一个动态的模块化框架.它向用户提供了模块化与插件化.面向服务构架和模块扩展支持等功能.该平台是OSGi联盟定义的服务平台规范移植到.NET的实现. 简介 尤埃开放服务平台是一个基于. ...
- MVC基础篇—控制器与视图数据的传递
Viewdata,Viewbag,Tempdata 1 Vewdata:简单来说就是数据字典,通过键值对的形式来存放数据.举例如下: //后台控制器代码: public ActionResult V ...
- code for qint function
function [p,y,a] = qint(ym1,y0,yp1) %QINT - quadratic interpolation of three adjacent samples % % [p ...
- 设计模式之策略模式(Strategy)
策略模式将不同算法的逻辑抽象接口封装到一个类中,通过组合和多态结合的方式来进行不同算法具体的实现. 作用 策略模式是一种定义一系列算法的方法,Strategy类层次为Context定义了一系列的可重用 ...
- SSM(Spring+SpringMvc+Mybatis)整合笔记
1.使用开发工具 jdk1.8 eclipse Tomcat7.0 MySql 2.创建数据库和表,由于重点是整合,所以数据库就随意加几条数据. 3.创建动态Web项目(推荐使用Maven可以用配置来 ...
- JAVA的高并发基础认知 二
一.JAVA高级并发 1.5JDK之后引入高级并发特性,大多数的特性在java.util.concurrent 包中,是专门用于多线程发编程的,充分利用了现代多处理器和多核心系统的功能以编写大规模并发 ...
- canvas-star7.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 2018-02-06 编程猫IDE体验:对Scratch的改进
前两天偶遇编程猫推介(为什么没有中文的编程?), 第一眼感觉像Scratch, 求证之下确实, 并且据说有改良. 今天非常粗浅地尝试一下, 限于水平没有做出很炫的效果, 不过颇有些发现. 首先上最终效 ...