IOS开发(49)之关于 self与内存相关的用法总结

来源:互联网 发布:淘宝靠谱的iphone店铺 编辑:程序博客网 时间:2024/05/01 15:29

今天新同事问了一些关于什么时候用全局变量,什么时候用self.赋值的问题,所以笔者在此说明一下。

何时使用self.在网上搜索或者论坛里的回复大多都是简简单单的说这与objc的存取方法有关,如何与存取方式有关究竟他们之间的是什么样的关系就很少有同学回答了。下面以代码来说明问题:

创建一个Student类,继承NSObject类,代码: 

01#import <Foundation/Foundation.h>
02  
03@ interface Student : NSObject{
04  
05    NSString *idd;
06    NSString *name;
07}
08@property (nonatomic, retain) NSString *idd;
09@property (nonatomic, retain) NSString *name;
10  
11@end

.m文件 代码:

01#import "Student.h"
02  
03@implementation Student
04@synthesize idd,name;
05  
06- (void)dealloc
07{
08    [idd release];
09    [name release];
10    [super dealloc];
11}
12  
13  
14

@end

使用@propety @synthesize实现Student的成员属性的set get方法。通常我们在其他类里访问Student的成员属性的做法:

获取student的名字通过student.name,给名字赋值[student setName:@“jordy”]; 其中student是Student类对象,如果在Student类内部访问其成员属性使用[self setName:@”jordy”], 访问使用self.name;

注意:上述的代码,由于wordpress的原因,代码中的字符会自动保存为中文格式。你在使用时记得改为英文格式。

在Student.h和Student.m文件,是我们习惯性的写法,但似乎还是不能解释什么加self和不加self的区别,请看下面代码,是另一种习惯性的写法,还以Student类为例:

.h文件 代码: 

01#import <Foundation/Foundation.h>
02  
03@ interface Student : NSObject{
04  
05    NSString *_idd;
06    NSString *_name;
07}
08@property (nonatomic, retain) NSString *idd;
09@property (nonatomic, retain) NSString *name;
10  
11

@end

 .m文件 代码:

01#import "Student.h"
02  
03@implementation Student
04@synthesize idd = _idd;
05@synthesize name = _name;
06  
07- (void)dealloc
08{
09    [_idd release];
10    _idd = nil;
11    [_name release];
12    _name = nil;
13    [super dealloc];
14}
15  
16  
17@end

可以注意到上述代码,与之前的代码,在.h文件name变量改写为了_name;在.m文件中@sythesize的写法也发生了变化。

如果通过方法self._name获取属性的值,xcode编译器会提示错误,其实这也就说明了,我们通常使用self.name实际使用的是student类name的get方法,同理name的set方法亦是如此。

接下来从内存管理来说明使用self.和不使用self的区别:

ViewController.h文件,使用Student类,代码如下:

01#import <UIKit/UIKit.h>
02class Student;
03  
04@ interface ViewController : UIViewController{
05     
06    Student *_student;
07}
08  
09@property (nonatomic, retain) Student *student;
10  
11

@end

ViewController.m文件,代码:

01#import "ViewController.h"
02#import "Student.h"
03  
04@implementation ViewController
05@synthesize student = _student;
06  
07- (void)didReceiveMemoryWarning
08{
09    [super didReceiveMemoryWarning];
10}
11  
12#pragma mark - View lifecycle
13  
14- (void)viewDidLoad
15{
16    [super viewDidLoad];
17}
18  
19- (void) dealloc
20{
21    [_student release];
22    _student = nil;
23    [super dealloc];
24}

其它的方法没有使用到,所以这里就不在显示了。

在ViewController.m的viewDidLoad方法中创建一个Student类的对象

1Student *mystudent = [[Student alloc] init];
2self.student = mystudent;
3[mystudent release];

这是相信有人会有疑问了,问什么创建student对象要这么复杂,似乎直接使用self.student = [[Student alloc] init]; 也没有问题,不加self有时也是挺正常的呀?

接下来就需要从内存角度来分析它们之间的区别了:

1、加self的方式: 

1Student *mystudent = [[Student alloc] init];       //mystudent 对象 retainCount = 1;
2self.student = mystudent;   //student 对象 retainCount = 2;
3[mystudent release];  //student 对象 retainCount = 1;

retainCount指对象引用计数,student的property 是retain 默认使用self.student引用计数+1。

2、不加self的方式

1Student *mystudent = [[Student alloc] init];       //mystudent 对象 retainCount = 1;
2student = mystudent;   //student 对象 retainCount = 1;
3[mystudent release];   //student 对象内存已释放,如果调用,会有异常

3、加self直接赋值方式

1self.student = [[Student alloc] init];  //student 对象 retainCount = 2;容易造成内存泄露

由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 病毒软件不断发信息买服务怎么办 qq被腾讯屏蔽了怎么办 电脑被u盘中毒怎么办 电脑一分钟重启怎么办 创维电视音量小怎么办 捷豹pin码忘记了怎么办 华为手机版本更新下载不了怎么办? 微信钱包没有钱怎么办 微信钱包里没有钱怎么办 微信没有收到退款怎么办 微信转账退款没有收到怎么办 礼物跟人家送重复怎么办 你已被steam封禁怎么办 武装突袭3被锁定怎么办 绝地求生右下角小地图变大怎么办 ctrl z 误删了怎么办 武装突袭3渴了怎么办 武装突袭3枪卡壳怎么办 玩武装突袭3CPU不好怎么办? 户户通001信号中断怎么办 电脑运行速度特别慢怎么办 win8.1电脑太卡怎么办 电线厂非法战地没拆够怎么办 久笔记本电脑玩彩虹六号卡怎么办 彩虹六号一直建立小队进不去怎么办 小佩喂食器离线怎么办 手机打游戏掉帧怎么办 电脑打游戏掉帧怎么办 武装突袭3太卡怎么办 英语b级考不过怎么办 绝地求生被燃烧瓶烧了怎么办 搜狗输入法打字出现问好怎么办 全民k歌解码失败怎么办 视频声音小怎么办调大 乐视2视频声音小怎么办 录视频声音太小怎么办 显卡装了没反应怎么办 笔记本关闭核显黑屏怎么办 驱动补丁被卸了怎么办 网络驱动被删了怎么办 新装系统网卡没驱动怎么办