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