001-weakとassignの違い
2230 ワード
1、テストシーン:画面をクリックしたときに赤いviewを追加する
2、weakのテスト #import "ViewController.h"
@interface ViewController ()
/** weak */
@property (nonatomic,weak) UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
_redView = redView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// frame
_redView.frame = CGRectMake(100, 100, 100, 100);
}
@end
#import "ViewController.h"
@interface ViewController ()
/** weak */
@property (nonatomic,weak) UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
_redView = redView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// frame
_redView.frame = CGRectMake(100, 100, 100, 100);
}
@end
#import "ViewController.h"
@interface ViewController ()
/** weak */
@property (nonatomic,weak) UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
//[self.view addSubview:redView];
_redView = redView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// frame
_redView.frame = CGRectMake(100, 100, 100, 100);
}
@end
3、テストassgin #import "ViewController.h"
@interface ViewController ()
@property (nonatomic,assign) UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
//[self.view addSubview:redView];
_redView = redView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// frame
_redView.frame = CGRectMake(100, 100, 100, 100);
}
@end
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,assign) UIView *redView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
//[self.view addSubview:redView];
_redView = redView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// frame
_redView.frame = CGRectMake(100, 100, 100, 100);
}
@end