Chinaunix首页 | 论坛 | 博客
  • 博客访问: 76663
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-30 14:22
文章分类

全部博文(48)

文章存档

2014年(47)

2013年(1)

我的朋友

分类: iOS平台

2014-01-20 15:11:37

  入门之如何时使用self?在ObjC的学习中经常会碰到是否应该使用self的苦恼,或者说什么时候使用全局变量,什么时候self?

  大多数的答案是:“这与objc的存取方法有关”

  怎么样才能有关呢?接下来通过几个小例子来看一下。

  首先我们创建一个学生类: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

  @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];

  这是相信有人会有疑问了,问什么创建student对象要这么复杂,似乎直接使用self.student

  = [[Student alloc] init]; 也没有问题,不加self有时也是挺正常的呀?

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

  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,为了确保当前类对此属性具有拥有权

  个人使用习惯:

  @interface CustomClass : UIViewController

  {

  NSString *str

  }

  @property (retain, nonatomic) NSString *str; @implementation CustomClass @synthesize str; -(void)viewDidLoad

  { //方法一 用alloc必须手动释放一次 self.str = [[NSString alloc]initWithString:@"my str"];

  [str release]; //方法二 用类方法不用 self.str = [NSString stringWithString:@"my str"];

  以后调用时直接使用str,不必使用self.str

  [str appendString:@"\n"];

  } //在dealloc中必须释放 - (void)dealloc

  { //方法一 [str release];

  str = nil; //方法二 self.str = nil;

  [super dealloc];

  }

  想要了解更多有关ios开发的知识可以查询:天地会。

阅读(383) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~