#####pch文件简介(摘抄自survivors的博客) 首先 pch 文件(即:Prefix Header)是一种预编译文件,在 Xcode 6 之前创建新的工程则会自动将该文件一起创建出来,但在 Xcode 6 之后苹果官方则默认将自动创建的方式变更为后续手动人工创建的方式;

该文件在项目工程中主要作用于将较常用且稳定的的类存放在其中,方便开发时共享其中的方法资源,不用多次在不同的类文件中引用其头文件.

但是有几点建议,因为该 pch 文件在预编译后会将头文件缓存起来,再次编译时则不需要重新编译该文件中的内容,进而提升了编译的速率,所有尽量不要将开发中共用性较低的文件或宏定义(宏定义可单独创建一个头文件进行存放,再将该宏文件引入至 pch)引入进 pch 文件中导致重复编译的操作,反而降低其速率使文件所带来的作用大大折扣. #####pch文件创建(摘抄自survivors的博客) 1.右键选择 New File 或使用快捷键 command+N 的方式,则会出现创建文件的界面,在右上方搜索框中输出"pch"字样,如下图所示:

2.选中 PCH File 文件,点击Next ->Save As中输入文件名 -> Create 创建便会生成一个 pch 文件.

注:该 pch 文件的命名方式,建议以项目名称开头,例如项目名称为"TestDemo"则 pch 文件名称则为"TestDemo-Prefix",当然实际命名以用途为准. #####pch配置(摘抄自survivors的博客) 创建完成后即可开始配置

#####什么是单例 一个单例类,在整个程序中只有一个实例,并且提供一个类方法供全局调用,在编译时初始化这个类,然后一直保存在内存中,到程序退出时由系统自动释放这部分内存。 #####单例的优缺点 优点: 1.在整个程序中只会实例化一次,所以在程序如果出了问题,可以快速的定位问题所在; 2.由于在整个程序中只存在一个对象,节省了系统内存资源,提高了程序的运行效率; 缺点: 1.单例不能继承,不易扩展 2.单例实例一旦创建,对象指针是保存在静态区的,那么在堆区分配空间只有在应用程序终止后才会被释放 #####一个简单传值的小例子 在ShareInstanceTest-PrefixHeader.pch文件中导入了单例类的头文件,这样就不用到处引用这个头文件了。

#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
复制代码

#####运行

转载于:https://juejin.im/post/5cb467895188251b274acdaf

Xcode pch文件配置及object c 单例创建相关推荐

  1. c# 多线程单例模式_单例模式,多线程单例,双重锁定单例,工场单例创建上下文...

    单例模式,多线程单例,双重锁定单例,工厂单例创建上下文. 单例子模式定义 保证一个类仅有一个实例,并提供一个访问它的全局访问点. 通常我们可以让一个全局变量使得一个对象被访问,但它不能防止你实例化多个 ...

  2. iOS单例创建的一点疑惑

    线程安全的单例常用写法, +(AccountManager *)sharedManager{static AccountManager *defaultManager = nil;disptch_on ...

  3. XCode6 ,iOS之PCH文件配置

    1: 创建PCH文件 NewFile-->Other中的PCH File-->Next-->Create 2:配置PCH文件 项目中的TARGETS-->Build Setti ...

  4. 自动注入、加载 properties 文件、scope 属性、单例设计模式

    一.自动注入 在 Spring 配置文件中对象名和 ref="id"id 名相同使用自动注入,可以不配置<property/> 两种配置办法 2.1 在<bean ...

  5. IOS 中 pch 文件详解

    在 Xcode6 之前,创建一个新工程会在 Supporting files 文件夹下面自动创建一个"工程名-Prefix.pch"文件,也是一个头文件,pch 头文件的内容能被项 ...

  6. spring注入的几种方式(文件配置以及注解方式)

    前言 spring利用IOC(控制反转)机制,将创建对象的权利交给了spring框架,从而降低程序的耦合.spring有文件配置和注解两种策略来实现Bean对象的创建和注入,这两种方式可以相互代替,后 ...

  7. 四大传值详解:属性传值,单例传值,代理传值,block传值

    一:属性传值 传值情景:从前一个页面向后一个页面传值 a.在后一个页面,根据传值类型和个数,写属性 b.在前一个页面, 为属性赋值 c.在后一个页面, 使用值 例如: 第一个视图: #import & ...

  8. 单例Singleton

    先提供一个完整版: // .h文件 @interface SingleTon : NSObject /** 获取单例对象*/ + (instancetype)sharedInstance; + (in ...

  9. 单例设计模式-容器单例

    基于容器的单例模式,与享元模式类似,我们也可以使用容器单例模式,来管理多个单例对象,那我们通过coding,debug,讲解的方式来学习一下 package com.learn.design.patt ...

最新文章

  1. 图的割点、桥与双连通分支
  2. TreeSet集合排序方式一:自然排序Comparable
  3. 简介struct cmsghdr结构
  4. 利用pylot进行性能测试
  5. 重置UNDO 表空间.
  6. phython在file同时写入两个_轻松支撑百万级数据点写入 京东智联云时序数据库HoraeDB架构解密...
  7. Hadoop随笔(一)
  8. HTML的HTTP 中 GET 与 POST 的区别
  9. java游戏怎么设置背景色_java-将背景色设置为JButton
  10. # SDN第五次上机作业
  11. Java执行外部命令,并把结果回显到控制台
  12. JDK、TOMCAT 配置环境变量
  13. pandas 表操作
  14. 批量下载魔兽replays录像文件
  15. 负一的n次方c语言,c语言 10 负次方
  16. 我是如何做测试项目管理的
  17. 【MySQL学习笔记(十六)】之redo日志超详细讲解
  18. 计算物品的良率(python)
  19. 转载-酷狗音乐API
  20. 【读论文】A Deep Neural Network for Unsupervised Anomaly Detection and Diagnosis in Multivariate Time...

热门文章

  1. php 找到行mysql_php – Mysql – gt;使用order by时获取行位置
  2. linux磁盘管fdisk,Linux 磁盘分区工具和挂载,fdisk管理分区详解
  3. 第 1 章 课程概述
  4. shell脚本文件中ll提示找不到命令
  5. CSS使表格不变形(原创)
  6. redis mysql windows_Redis+Mysql模式和内存+硬盘模式的异同
  7. 903计算机技术综合基础,北大903计算机技术综合基础考研真题、资料、参考书
  8. mysql 5.7.13 log_有关binlog的那点事(二)(mysql5.7.13)
  9. python server酱_用Python抢到回家的车票,so easy
  10. python asyncio tcp转发_asyncio不通过tcp发送整个图像数据