• Day4


    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

    2 block的使用

    -(void)downloadClick:(UIButton *)button{
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(80, 400, 160, 40)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.backgroundColor = [UIColor lightGrayColor];
        NSDictionary *dict = self.appList[button.tag];
        lable.text = [NSString stringWithFormat:@"下载%@完成",dict[@"name"]];
        
        lable.font = [UIFont systemFontOfSize:13.0];
        lable.alpha = 0.0;
        [self.view addSubview:lable];
        
        //动画效果
        //[UIView beginAnimations:nil context:nil];
        //[UIView setAnimationDuration:1.0];
        //lable.alpha = 1.0;
        //[UIView commitAnimations];
        
        [UIView animateWithDuration:1.0 animations:^{
            lable.alpha=1.0;
        } completion:^(BOOL finished) {
            [lable removeFromSuperview];
        }];
        
        
    }
    

      

     

  • 相关阅读:
    COJ 1691:前缀和
    COJ 1692:模拟
    POJ 3415:后缀数组+单调栈优化
    python拓展知识
    git总结
    好的文章及博客收集
    python爬虫总结
    ajax与java后台交互
    Java多线程与并发库
    Django框架创建
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4342573.html
Copyright © 2020-2023  润新知