#####pch文件简介(摘抄自survivors的博客) 首先 pch 文件(即:Prefix Header)是一种预编译文件,在 Xcode 6 之前创建新的工程则会自动将该文件一起创建出来,但在 Xcode 6 之后苹果官方则默认将自动创建的方式变更为后续手动人工创建的方式;
该文件在项目工程中主要作用于将较常用且稳定的的类存放在其中,方便开发时共享其中的方法资源,不用多次在不同的类文件中引用其头文件.
但是有几点建议,因为该 pch 文件在预编译后会将头文件缓存起来,再次编译时则不需要重新编译该文件中的内容,进而提升了编译的速率,所有尽量不要将开发中共用性较低的文件或宏定义(宏定义可单独创建一个头文件进行存放,再将该宏文件引入至 pch)引入进 pch 文件中导致重复编译的操作,反而降低其速率使文件所带来的作用大大折扣. #####pch文件创建(摘抄自survivors的博客) 1.右键选择 New File 或使用快捷键 command+N 的方式,则会出现创建文件的界面,在右上方搜索框中输出"pch"字样,如下图所示:
注:该 pch 文件的命名方式,建议以项目名称开头,例如项目名称为"TestDemo"则 pch 文件名称则为"TestDemo-Prefix",当然实际命名以用途为准. #####pch配置(摘抄自survivors的博客) 创建完成后即可开始配置
#ifndef ShareInstanceTest_PrefixHeader_pch
#define ShareInstanceTest_PrefixHeader_pch
// Include any system framework and library headers here that should be included in all compilation units.
//包括所有编译单元中应该包含的任何系统框架和库头。
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
//您还需要设置一个或多个目标的前缀头构建设置以引用此文件。
#import "App.h"
#endif
复制代码
#####单例的创建
// App.h
#import <Foundation/Foundation.h>
@interface App : NSObject
@property (nonatomic, copy)NSString *name;
+(instancetype)sharedInstance;
@end
复制代码
// App.m
#import "App.h"
@interface App(){
}
@end
@implementation App
+(instancetype)sharedInstance{
static App *sharedInstance = nil;
static dispatch_once_t onceToken;
//void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
//该函数会保证相关的块必定会执行,而且只执行一次,这个方法是完全线程安全的。
dispatch_once (&onceToken, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
@end
复制代码
#####其他类的代码
// AppDelegate.h
// sharedInstanceTest
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
复制代码
// AppDelegate.m
// sharedInstanceTest
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *viewController = [[ViewController alloc]init];
UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
复制代码
// ViewController.h
// sharedInstanceTest
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
复制代码
// ViewController.m
// sharedInstanceTest
#import "ViewController.h"
#import "logControllerViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[App sharedInstance].name = @"这是单例的值";
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat labelWidth = 180;
CGFloat labelheigth = 20;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((screen.size.width - labelWidth)/2, (screen.size.height - labelheigth)/2, labelWidth, labelheigth)];
//设置文本
label.text = @"ViewController";
//设置文本左右居中
label.textAlignment = NSTextAlignmentCenter;
//设置文本颜色
label.textColor = [UIColor redColor];
//视图中添加标签
[self.view addSubview:label];
//初始化按钮并设置样式
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//设置常态下按钮的标题
[button setTitle:@"点击跳转" forState:UIControlStateNormal];
button.frame = CGRectMake((screen.size.width - 100)/2, label.frame.origin.y + labelheigth, 100, 30);
//为按钮添加事件,第一个参数addTarget,即事件处理者;第二个参数action,即事件处理方法;第三个参数forControlEvents,即事件。
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)click{
logControllerViewController *log = [[logControllerViewController alloc]init];
[self.navigationController pushViewController:log animated:NO];
}
@end
复制代码
// logControllerViewController.h
// sharedInstanceTest
#import <UIKit/UIKit.h>
@interface logControllerViewController : UIViewController
@end
复制代码
// logControllerViewController.m
// sharedInstanceTest
#import "logControllerViewController.h"
@interface logControllerViewController ()
@end
@implementation logControllerViewController
- (void)viewDidLoad {
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat labelWidth = 180;
CGFloat labelheigth = 20;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((screen.size.width - labelWidth)/2, (screen.size.height - labelheigth)/2, labelWidth, labelheigth)];
label.text = [App sharedInstance].name;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor redColor];
[self.view addSubview:label];
}
@end
复制代码
#####运行