标签:应用 highlight ace undle nil 检查 oid 官方 消息
Objective-C 有两个神奇的方法:+load 和 +initialize,这两个方法在类被使用时会自动调用。但是两个方法的不同点会导致应用层面上性能的显著差异。
#pragram ---main函数中的代码---
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
NSLog(@"%s",__func__);
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
#pragram ---基于NSObject的Person类---
#import "Person.h"
@implementation Person
+ (void)load{ NSLog(@"%s",__func__); }
+ (void)initialize{
[super initialize];
NSLog(@"%s %@",__func__,[self class]);
}
- (instancetype)init{
if (self = [super init]){
NSLog(@"%s",__func__);
}
return self;
}
@end
#pragram ---基于Person的Son类---
#import "Girl.h"
@implementation Girl
+ (void)load{
NSLog(@"%s ",__func__);
}
+ (void)initialize{
[super initialize];
NSLog(@"%s ",__func__);
}
- (instancetype)init{
if (self = [super init]){
NSLog(@"%s",__func__);
}
return self;
}
@end
2015-10-27 15:21:07.545 initialize[11637:334237] +[Person load] 2015-10-27 15:21:07.546 initialize[11637:334237] +[Girl load] 2015-10-27 15:21:07.546 initialize[11637:334237] main
#import "ViewController.h"
#import "Person.h"
#import "Son.h"
#import "Girl.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person * a = [Person new];
Person * b = [Person new];
Girl *c = [Girl new];
Girl *d = [Girl new];
}
@end
2015-10-27 15:33:56.195 initialize[11711:342410] +[Person load] 2015-10-27 15:33:56.196 initialize[11711:342410] +[Girl load] 2015-10-27 15:33:56.197 initialize[11711:342410] main 2015-10-27 15:33:56.259 initialize[11711:342410] +[Person initialize] Person 2015-10-27 15:33:56.259 initialize[11711:342410] -[Person init] 2015-10-27 15:33:56.259 initialize[11711:342410] -[Person init] 2015-10-27 15:33:56.259 initialize[11711:342410] +[Girl initialize] 2015-10-27 15:33:56.260 initialize[11711:342410] -[Girl init] 2015-10-27 15:33:56.260 initialize[11711:342410] -[Girl init]
#pragram ---ViewController 中的代码---
#import "ViewController.h"
#import "Person.h"
#import "Son.h"
#import "Girl.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person * a = [Person new];
Person * b = [Person new];
Son*z = [Son new];
}
@end
2015-10-27 15:44:55.762 initialize[12024:351576] +[Person load] 2015-10-27 15:44:55.764 initialize[12024:351576] +[Son load] 2015-10-27 15:44:55.764 initialize[12024:351576] +[Girl load] 2015-10-27 15:44:55.764 initialize[12024:351576] main 2015-10-27 15:44:55.825 initialize[12024:351576] +[Person initialize] Person 2015-10-27 15:44:55.825 initialize[12024:351576] -[Person init] 2015-10-27 15:44:55.825 initialize[12024:351576] -[Person init] 2015-10-27 15:44:55.826 initialize[12024:351576] +[Person initialize] Son 2015-10-27 15:44:55.826 initialize[12024:351576] -[Person init]
iOS-方法之+ initialize 与 +load(转载)
标签:应用 highlight ace undle nil 检查 oid 官方 消息
原文地址:http://www.cnblogs.com/lxlx1798/p/7111880.html