黑马程序员——oc之self的使用
2015-09-10 23:41
543 查看
-------android培训、java培训、iOS培训、.Net培训、期待与您交流!
----------
首先我们创建一个学生类:Student类
这个学生类里有学生的id和学生的姓名name
#import
@interface
Student : NSObject{
//idname
NSString *id;
NSString *name;
}
@property
(nonatomic,strong) NSString *id;
@property
(nonatomic,strong) NSString *name;
@end
学生类的实现文件
#import
"Student.h"
@implementation
Student
147c9
@synthesize
id,name;
@end
如果使用上面的方法来定义学生类的属性的get、set方法的时候,那么其他类访问的时候就是:
获取student的名字通过student.name来获取,给名字赋值则使用[student
setName:@“eve”]; 其中student是Student类的对象,如果在Student类内部访问其成员属性使用[self
setName:@”evo”], 访问使用self.name;
上面的方法只是一种,但是很难解释self该不该使用。请看下面:
我们改写Student类
#import
@interface
Student : NSObject{
//idname
NSString *_id;
NSString *_name;
}
@property
(nonatomic,strong) NSString *id;
@property
(nonatomic,strong) NSString *name;
@end
.m文件
#import
"Student.h"
@implementation
Student
@synthesize
id = _id;
@synthesize
name = _name;
@end
可见这样的写法我们增加了_id和_name,其中@synthesize也有一定的变化。
如何这个时候使用self.name编译器就会报错,这样就说明了我们通常使用self.name实际使用的是student类name的get方法,同理name的set方法亦是如此。
另外网络上也有人从内存管理方面来说明的,我将其剪切出来以供学习:
ViewController.h文件,使用Student类,代码如下:
#import
@
class Student;
@
interface ViewController : UIViewController{
Student *_student;
}
@property
(nonatomic, retain) Student *student;
@end
ViewController.m文件,代码:
#import
"ViewController.h"
#import
"Student.h"
@implementation
ViewController
@synthesize
student = _student;
-
(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma
mark - View lifecycle
-
(void)viewDidLoad
{
[super viewDidLoad];
}
-
(void) dealloc
{
[_student release];
_student = nil;
[super dealloc];
}
其它的方法没有使用到,所以这里就不在显示了。
在ViewController.m的viewDidLoad方法中创建一个Student类的对象
Student
*mystudent = [[Student alloc] init];
self.student
= mystudent;
[mystudent
release];
接下来就需要从内存角度来分析它们之间的区别了:
1、加self的方式:
Student
*mystudent = [[Student alloc] init]; //mystudent 对象
retainCount = 1;
self.student
= mystudent; //student 对象 retainCount = 2;
[mystudent
release];//student 对象 retainCount = 1;
retainCount指对象引用计数,student的property
是retain 默认使用self.student引用计数+1。
2、不加self的方式
Student
*mystudent = [[Student alloc] init]; //mystudent 对象
retainCount = 1;
student
= mystudent; //student 对象 retainCount = 1;
[mystudent
release]; //student 对象内存已释放,如果调用,会有异常
3、加self直接赋值方式
self.student = [[Student alloc] init];//student 对象 retainCount =
2;容易造成内存泄露
由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存
个人总结:只需要在属性初始化的时候使用self.属性,其他时候直接使用属性名就行;使用self.是 使retaincount+1,为了确保当前类对此属性具有拥有权
----------
首先我们创建一个学生类:Student类
这个学生类里有学生的id和学生的姓名name
#import
@interface
Student : NSObject{
//idname
NSString *id;
NSString *name;
}
@property
(nonatomic,strong) NSString *id;
@property
(nonatomic,strong) NSString *name;
@end
学生类的实现文件
#import
"Student.h"
@implementation
Student
147c9
@synthesize
id,name;
@end
如果使用上面的方法来定义学生类的属性的get、set方法的时候,那么其他类访问的时候就是:
获取student的名字通过student.name来获取,给名字赋值则使用[student
setName:@“eve”]; 其中student是Student类的对象,如果在Student类内部访问其成员属性使用[self
setName:@”evo”], 访问使用self.name;
上面的方法只是一种,但是很难解释self该不该使用。请看下面:
我们改写Student类
#import
@interface
Student : NSObject{
//idname
NSString *_id;
NSString *_name;
}
@property
(nonatomic,strong) NSString *id;
@property
(nonatomic,strong) NSString *name;
@end
.m文件
#import
"Student.h"
@implementation
Student
@synthesize
id = _id;
@synthesize
name = _name;
@end
可见这样的写法我们增加了_id和_name,其中@synthesize也有一定的变化。
如何这个时候使用self.name编译器就会报错,这样就说明了我们通常使用self.name实际使用的是student类name的get方法,同理name的set方法亦是如此。
另外网络上也有人从内存管理方面来说明的,我将其剪切出来以供学习:
ViewController.h文件,使用Student类,代码如下:
#import
@
class Student;
@
interface ViewController : UIViewController{
Student *_student;
}
@property
(nonatomic, retain) Student *student;
@end
ViewController.m文件,代码:
#import
"ViewController.h"
#import
"Student.h"
@implementation
ViewController
@synthesize
student = _student;
-
(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma
mark - View lifecycle
-
(void)viewDidLoad
{
[super viewDidLoad];
}
-
(void) dealloc
{
[_student release];
_student = nil;
[super dealloc];
}
其它的方法没有使用到,所以这里就不在显示了。
在ViewController.m的viewDidLoad方法中创建一个Student类的对象
Student
*mystudent = [[Student alloc] init];
self.student
= mystudent;
[mystudent
release];
接下来就需要从内存角度来分析它们之间的区别了:
1、加self的方式:
Student
*mystudent = [[Student alloc] init]; //mystudent 对象
retainCount = 1;
self.student
= mystudent; //student 对象 retainCount = 2;
[mystudent
release];//student 对象 retainCount = 1;
retainCount指对象引用计数,student的property
是retain 默认使用self.student引用计数+1。
2、不加self的方式
Student
*mystudent = [[Student alloc] init]; //mystudent 对象
retainCount = 1;
student
= mystudent; //student 对象 retainCount = 1;
[mystudent
release]; //student 对象内存已释放,如果调用,会有异常
3、加self直接赋值方式
self.student = [[Student alloc] init];//student 对象 retainCount =
2;容易造成内存泄露
由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存
个人总结:只需要在属性初始化的时候使用self.属性,其他时候直接使用属性名就行;使用self.是 使retaincount+1,为了确保当前类对此属性具有拥有权
相关文章推荐
- ButterKnife Android程序员的一大利器
- 黑马程序员--学习笔记--IO流File文件操作
- 黑马程序员——oc之Foundation框架
- Android面试自我介绍
- 剑指Offer第四章面试题(Java版)
- 面试必备:文本框与按钮的最简组合
- 面试题5:从尾到头打印链表
- 初级程序员学习网址搜集(持续更新)
- 黑马程序员—–UDP协议传输
- AdaultBird--我的黑马程序员之路!Chapter10---java反射!
- 《程序员面试金典》--空格替换
- 面积最大的全1子矩阵(腾讯2012年暑期实习生招聘面试二面试题)
- 黑马程序员_java09_集合框架
- 机器学习笔试面试系列算法集锦
- 最适合程序员编程的10款字体
- 面试题二叉树相关问题总结
- 程序员必须开始的道路
- iOS开发:iOS常见面试题
- 黑马程序员_java08_多线程
- 常见的数据库基础面试题大全