@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 ...
随机推荐
- 从零开始学 Web 之 ES6(三)ES6基础语法一
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- PHP-CPP开发扩展(五)
PHP-CPP是一个用于开发PHP扩展的C++库.本节讲解如何在C++中实现PHP类. 类和对象 类和对象 怎样在PHP-CPP里写出PHP的类呢?很简单,看下面的例子: main.cpp /** * ...
- Ubuntu14.04 编译 Android 5.1.1源码(采用国内镜像)
欢迎转载,转载请注明出处: http://www.cnblogs.com/lanrenxinxin/p/5424554.html 之前就有编译android源码的想法,但是由于有GFW的存在,又没有梯 ...
- 第4章 Selenium2-java WebDriver API (三)
4.12 上传文件 4.12.1 sendKeys实现上传 html <html> <head> </head> <body> <div ...
- numpy使用指南
numpy.array numpy.array是numpy中用于处理n阶数组的对象,是其类族中的重要基类. numpy.array可以表示任意维的数组,可以使用构造函数初始化: arr = numpy ...
- Django组件之cookie与session
一.引子 http协议是无状态的,就是它不会记录请求和响应的任何信息,比如你访问一个服务器的一个网页时,先要你登录一下,然后进入网页,但当你要进入这个服务器的另一个网页时,它照常不会知道刚才你已经登录 ...
- “未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序”问题的解决
发布到win7 64位旗舰版iis上时,报:“未能加载文件或程序集“BC.Common”或它的某一个依赖项.试图加载格式不正确的程序”. 该DLL的本地复制没有设置为true(在项目引用里找到该引用, ...
- java——线程
线程与进程 1.线程:程序中单独顺序的控制流 线程本身是通过程序进行运行 线程是程序中的顺序控制流,只能使用分配给程序的资源与环境 2.进程:执行中的程序 一个进程可以包含一个或多个线程 一个进程至少 ...
- 关于模板引擎handlebars.js基本用法
说明:模板引擎主要针对于渲染DOM,取代了字符串拼接,用下面的代码亲测handlebars模板引擎比字符串拼接渲染DOM慢了20ms, 这里配置一个在线DEMO,简单说明下handlebars.js的 ...
- thinkphp链接多个数据库时怎么调用M方法?
老项目tp3.1.3,有N个数据库,thinkphp好久没用了,不知道怎么用M方法了,代码测验成功! 数据库名称: 2.直接上代码 $custom = M('base','branch_','shop ...