码迷,mamicode.com
首页 > 其他好文 > 详细

Day4

时间:2015-03-16 20:49:40      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

1 Strong and Weak

#import "Person.h"

@implementation Person

- (void)dealloc
{
    NSLog(@"person destroy");
}

@end

强指针:strong系统一般不会自动释放

弱指针:weak 系统会立即释放对象

//
//  ViewController.m
//  BasicKnowledge
//
//  Created by xin on 15-3-16.
//  Copyright (c) 2015年 Jackey. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (nonatomic,strong) Person *person;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //strong pointer
    //won‘t release the memory
    //默认的是强指针
    //不执行销毁语句
    self.person = [[Person alloc]init];
    
    //这里是强指针,但是这是个局部对象,在viewdidload执行完后会执行销毁语句
    Person *person1 = [[Person alloc]init];
    
    //立即释放
    __weak Person *person2 = [[Person alloc]init];
    //strong weak 的区别:
    //strong为持有者,weak为观望者,就像strong是一条狗链子
    NSLog(@"-----");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
//
//  ViewController.m
//  BasicKnowledge
//
//  Created by xin on 15-3-16.
//  Copyright (c) 2015年 Jackey. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (nonatomic,strong) Person *person;
@property (nonatomic,weak) Person *weakPerson;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //strong pointer
    //won‘t release the memory
    //默认的是强指针
    //不执行销毁语句
    //体现strong和weak的区别
    //strong 为持有者,牵着person对象,weakperson为虚指向person对象
    //下面的情况,weakperson不会被自动销毁
    self.person = [[Person alloc]init];
    self.weakPerson = self.person;
    
    NSLog(@"-----");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

weak指针一般指向strong pointer的对象

    //拖拉的控件用weak
    //属性对象用strong
    //整数非对象类型用assign
  //NSString 用copy

  

Day4

标签:

原文地址:http://www.cnblogs.com/lihaozhou/p/4342573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!