主要参考文章:http://blog.sina.com.cn/s/blog_a263f0c601010qj9.html
橙色字体为自己标注的的内容,方便加深印象
IPHONE开发SELF的用法
关于什么时间用self. ,其实是和Obj-c的存取方法有关,不过网上很多人也都这么解答的,那它为什么和存取方法有关?怎么有关的?并没有多少人回答出来.同时关于内存管理的内容,请大家看旺财勇士的Objective-C内存管理总结~CC专版,有些东西我就不多解释了.<wbr style="line-height:25px"></wbr>
进入正题,我们经常会在官方文档里看到这样的代码:<wbr style="line-height:25px"></wbr>
MyClass.h<wbr style="line-height:25px"></wbr>
[/lang]<wbr style="line-height:25px"></wbr>
@interface MyClass : NSObject {<wbr style="line-height:25px"></wbr>
MyObject *myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
@property (nonatomic, retain) MyObject *myObject;<wbr style="line-height:25px"></wbr>
@end<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyClass.m<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
@synthesize myObject;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(id)init{<wbr style="line-height:25px"></wbr>
if(self = [super init]){<wbr style="line-height:25px"></wbr>
MyObject * aMyObject = [[MyObject alloc] init];<wbr style="line-height:25px"></wbr>
self.myObject = aMyObject;<wbr style="line-height:25px"></wbr>
[aMyObject release];<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
return self;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
有人就问,为什么要这么复杂的赋值?为什么要加self. ?直接写成self.myObject
= [[MyObject alloc] init];不是也没有错么?不加self有时好像也是正常的?<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
现在我们来看看内存管理的内容:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
先看间接赋值的:<wbr style="line-height:25px"></wbr>
1.加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;<wbr style="line-height:25px"></wbr>
self.myObject = aMyObject; //myObject retainCount = 2;<wbr style="line-height:25px"></wbr>
[aMyObject release];//myObject retainCount = 1;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
2.不加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;<wbr style="line-height:25px"></wbr>
myObject = aMyObject; //myObject retainCount = 1;<wbr style="line-height:25px"></wbr>
[aMyObject release];//对象己经被释放<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
再看直接赋值的:<wbr style="line-height:25px"></wbr>
3.加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
self.myObject = [[MyObject alloc] init]; //myObject retainCount = 2;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
4.不加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
myObject = [[MyObject alloc] init]; //myObject retainCount = 1;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
现在是不是有点晕,我们先来把代码改一下,官方的一种常见写法:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyClass.h<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
@interface MyClass : NSObject {<wbr style="line-height:25px"></wbr>
MyObject * _myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
@property (nonatomic, retain) MyObject *myObject;<wbr style="line-height:25px"></wbr>
@end<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyClass.m<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
@synthesize myObject = _myObject;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
OK,你现在再试下,如果你用self._myObject
= aMyObject;或者myObject = aMyObject;你会得到一个错误,为什么呢,这里就是和Obj-c的存取方法有关了.说白了很简单,大家都知道,
@property (nonatomic, retain) MyObject *myObject;是为一个属性设置存取方法,只是平时我们用的方法名和属性名是一样的,现在你把它写成不同的名字,就会很清楚了.
_myObject是属性本身, myObject是存取方法名.<wbr style="line-height:25px"></wbr>
一般来说self点出来的都是方法这个好办,
但是标准的TAB项目的Delegate的m文件里会出现这样的代码
@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
- (void)dealloc
{
[_window release];
[_tabBarController release];
[super dealloc];
}
之前一直很晕的问题是为什么释放的是_window 而不是window 也查过的问题是window = _window这种但是很多地方的解释是window是_window的别名,或者说把window定义成_window的别名。但是这里的解释能够更清楚地解答我的疑问了,@property和@synthesize是声明是实现一个属性的存取方法。@property是定义属性说明需要存取方法 @synthesize是合成存取器是告诉编译器生成属性的get和set方法。以往的感觉是定义了属性就会出现与之同名的变量
但是现在来看其实只是有个方法,如果没有定义或者赋值到本体的内部成员变量上那么会自动成圣与之同名的变量,如果赋值了那么则证明 如window = _window则说明属性window实际操作的是内部成员的_window. 也就是说 @property定义的是方法名@synthesize是让编译器自动生成方法。同时这里还解决了我一个问题就是关于readonly只读属性的赋值问题,尤其是这种带这window=_window的,现在来看是不生成SET方法,而读取方法返回的实际_window。
现在我们知道self.是访问属性的存取方法了,那存取方法又怎么工作的? self.myObject = [[MyObject alloc] init];为什么会有内存泄露?<wbr style="line-height:25px"></wbr>
关于nonatomic我不多解释了,它不是我要讲的重点,而且我也没完全搞清楚,不误导大家.
关于nonatomic我这也是一敲不通啊,目前还没用过atomicity模式的属性。
我只说assign, retain ,copy.<wbr style="line-height:25px"></wbr>
get方法是:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(MyObject*)myObject{<wbr style="line-height:25px"></wbr>
return _myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
Set方法是:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
// assign<wbr style="line-height:25px"></wbr>
-(void)setMyObject:(id)newValue{<wbr style="line-height:25px"></wbr>
_myObject = newValue;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
// retain<wbr style="line-height:25px"></wbr>
-(void)setMyObject:(id)newValue{<wbr style="line-height:25px"></wbr>
if (_myObject != newValue) {<wbr style="line-height:25px"></wbr>
[_myObject release];<wbr style="line-height:25px"></wbr>
_myObject = [newValue retain];<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
// copy<wbr style="line-height:25px"></wbr>
-(void)setMyObject:(id)newValue{<wbr style="line-height:25px"></wbr>
if (_myObject != newValue) {<wbr style="line-height:25px"></wbr>
[_myObject release];<wbr style="line-height:25px"></wbr>
_myObject = [newValue copy];<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
其实这些方法里还有别的内容,并不只是这些.而且这些方法可以被重写.比如你写一个<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(MyObject*)myObject{<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
return _myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
放在你的类里,你调用self.myObject时(不要把它放在等号左边,那会调用get方法)就会调用这个方法.<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
这里多说一句, @property是为你设置存取方法,和你的属性无关,你可以只写一句<wbr style="line-height:25px"></wbr>
看来理解对了 和属性本身无关 就是定义的访问方法名
<wbr style="line-height:25px"></wbr>
@property (readonly) NSString *name;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
在你的类里实现<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(NSString*)name{<wbr style="line-height:25px"></wbr>
NSLog(@"name");<wbr style="line-height:25px"></wbr>
return @"MyClass";<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
同样可以用self.name调用.<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
现在回头说说我们开始的那四个赋值,当不用self.的时候,那句话只是一般的赋值,把一个指针赋给另一个指针,不会对分配的内存有任何影响,所以2中不要最后[aMyObject
release];这句话和4是一回事.这里就不多说了.我们看看1和3,<wbr style="line-height:25px"></wbr>
当调用setMyObject:方法时,对newValue做了一次retain操作,我们必须把原来的newValue释放掉,不然就会内存泄露,在1里,我们有个aMyObject可以用来释放,在3里,我们无法释放它,所以,在3里,我们会多出来一个retainCount.内存泄露了.<wbr style="line-height:25px"></wbr>
self.myObject = [[MyObject alloc] init]会造成的内存泄露是说self点的时候一个引用然后alloc以后又会有一个 所以在这里一下增加的是两个引用计数。
<wbr style="line-height:25px"></wbr>
说了这么多,我只想让大家清楚,什么是调用属性本身,什么是调用存取方法.怎么样才能避免内存泄露,而且,以上例子里是在自己类里的调用,如果这个类被别的
分享到:
相关推荐
我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结。... iOS8 self-sizing cell UITableView+FDTemplateLayoutCell如何用一句话解决高
* 动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法. * * @see CAAnimationDelegate (按下command键点击) */ animation.delegate = self; /** duration * *...
本文将给大家总结介绍关于iOS中多线程的一些经典崩溃,下面话不多说了,来一起看看详细的介绍吧。 0x0 Block 回调的崩溃 在MRC环境下,使用Block 来设置下载成功的图片。当self释放后,weakSelf变成野指针,接着就...
众所周知iOS11正式版终于来了,最近也把app适配了一下,其实也不是很麻烦,来看看我做的一些操作,话不多说了,来一起看看吧。 1、UITableView、UICollectionView的变化 tableView在iOS11默认使用Self-Sizing,...
_slideSwitch = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) Titles:titles viewControllers:viewControllers]; //设置代理 _...
loadView默认做的事情是:如果此ViewController存在一个对应的nib文件,那么就加载这个nib。否则,就创建一个UIView对象。 如果你用Interface Builder来创建界面,那么不应该重载这个方法。 控制器的loadView方法...
可列出Listable是适用于iOS的声明性列表框架,可让您简洁地创建丰富,实时更新的基于列表的布局,这些布局可在多个轴上高度自定义:填充,间距,列数,对齐方式等。它旨在提高性能:处理列表大多数设备上没有超过10k...
ET蓝牙打印机管理器 ...通过这些委托方法,您可以做一些事情。 - (void)bluetoothStatusHasChange:(BOOL)isOn; - (void)didDiscoverPeripherals:(NSArray *)peripherals; - (void)didConnectPeripheral;
源码iOS8SelfSizingCells,在iOS8以下,如果需要实现一个不同高度的Cell,那需要你手动动态计算高度,这便是一个繁琐的事情,而且富有挑战性,需要把空间复杂度转换为时间复杂度之类的优化。而在iOS8新的SDK里面提供...
如何使用HealthKit 表示如何使用healthkit的示例,两个功能是HKQuantityType和HKCharacteristicType... [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes完成:^(BOOL
这是一个LCNewFeature快速集成新特性界面案例,源码LCNewFeature,每次拿到一个项目的时候,头疼的几件事之一就是新特性界面,写一堆代码做一个简单的东西。所以抽空写了个快速集成新特性界面的框架,传了上来。 ...
//所以往第一个界面上添加一个label,看标签是否会显示一些内容 UILabel *label = [[UILabel alloc]init]; label.frame = CGRectMake(0, 250, 300, 200); label.numberOfLines = 0; label.textColor = [UIColor...
每个ios开发者对loadView和viewDidLoad肯定都很熟悉,虽然这两个函数使用上真的是非常简单,但是和类似的initWithNibName/awakeFromNib/initWithCoder放在一起还是非常容易让人混淆的. 昨天看了下苹果官方的相关文档...
网络平安不只是软件厂商的事 今年上半年,网络平安软件及效劳厂商——趋势科技与网络业界领导厂商——思科系统公司 在北京共同宣布签署了为企业提供综合性病毒和蠕虫爆发防御解决方案的合作协议。该 协议进一步扩展...
///做你想做的事 } }]; ZPTextField *textField = [[ZPTextField alloc] initWithFrame:CGRectMake(50, 50, 200, 50)]; textField.NBregEx = REGEX_POWER_TELPHOONE_NUMBER; [self.view addSubview:...
EIGRP汇总的网络越多,主收敛发生时需要做的事情越少。 4.重复的路由ID EIGRP只是为了外部路由而使用路由器ID的概念来防止环路。EIGRP基于路由器上回环接口的最大IP地址来选择路由器ID.如果路由器没有回环接口...
源码LKAlarmManager, 方便快捷的把 “您的提醒” 加入到 日历或者本地通知中,会自动处理本地通知超过64个的情况,注册下 LKAlarmManager 回调,在接收到提醒的时候,就可以做你想做的事。 使用方法: 使用例子 :...
实现魔方游戏效果。即由很多图片拼成的格子中,每行每列都可以左右循环移动。 开发者说:这是很久之看过的一个... [self.view addSubview:gridView]; [gridView release]; 如有需优化和不足的地方,望高手指正。