美文网首页
Xcode pch文件配置及object - c 单例创建

Xcode pch文件配置及object - c 单例创建

作者: 寻心_0a46 | 来源:发表于2019-02-17 15:34 被阅读0次

pch文件简介(摘抄自survivors的博客)

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

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

但是有几点建议,因为该 pch 文件在预编译后会将头文件缓存起来,再次编译时则不需要重新编译该文件中的内容,进而提升了编译的速率,所有尽量不要将开发中共用性较低的文件或宏定义(宏定义可单独创建一个头文件进行存放,再将该宏文件引入至 pch)引入进 pch 文件中导致重复编译的操作,反而降低其速率使文件所带来的作用大大折扣.

pch文件创建(摘抄自survivors的博客)

1.右键选择 New File 或使用快捷键 command+N 的方式,则会出现创建文件的界面,在右上方搜索框中输出"pch"字样,如下图所示:

屏幕快照 2019-02-17 下午1.37.09.png

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

注:该 pch 文件的命名方式,建议以项目名称开头,例如项目名称为"TestDemo"则 pch 文件名称则为"TestDemo-Prefix",当然实际命名以用途为准.

pch配置(摘抄自survivors的博客)

创建完成后即可开始配置:

屏幕快照 2019-02-17 下午1.47.22.png

什么是单例

一个单例类,在整个程序中只有一个实例,并且提供一个类方法供全局调用,在编译时初始化这个类,然后一直保存在内存中,到程序退出时由系统自动释放这部分内存。

单例的优缺点

优点:

  • 1.在整个程序中只会实例化一次,所以在程序如果出了问题,可以快速的定位问题所在;
  • 2.由于在整个程序中只存在一个对象,节省了系统内存资源,提高了程序的运行效率;

缺点:

  • 1.单例不能继承,不易扩展;
  • 2.单例实例一旦创建,对象指针是保存在静态区的,那么在堆区分配空间只有在应用程序终止后才会被释放;

一个简单传值的小例子

屏幕快照 2019-02-17 下午3.22.58.png

在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

运行

Untitled.gif

相关文章

  • Xcode pch文件配置及object - c 单例创建

    pch文件简介(摘抄自survivors的博客) 首先 pch 文件(即:Prefix Header)是一种预编译...

  • Xcode6及之后为什么要去掉PCH文件

    Xcode6及之后的Xcode版本,创建新工程默认不再创建PCH文件,但是仍可以手动配置PCH文件,具体手动配置方...

  • pch文件

    PCH文件配置 Xcode6及以后版本才能手动配置PCH文件,以前版本都是由Xcode自动生成。作用:放一些公用宏...

  • iOS 创建pch文件

    一、手动创建和配置PCH文件 PCH文件的命名一般和它所在的项目同名。新建完PCH文件以后,先在Xcode中对其进...

  • iOS中PCH文件的使用

    1. PCH简介 Xcode6之前创建的工程都自带PCH文件,Xcode6之后创建的工程默认不带PCH文件,苹果可...

  • Xcode8 pch文件的配置

    # Xcode8 pch的配置 ## pch文件的设置 ### `$(SRCROOT)/+工程名+pch文件名字....

  • 创建pch文件

    对于Xcode6.0之后新建工程不再包含.pch文件的情况,可以手动创建一个pch文件,创建步骤: 新建pch文件...

  • Xcode升级后iOS11及iOS13一些问题汇总

    1、首先补充下创建pch文件的内容 1.1 创建pch文件 1.2 配置pch 搜索Prefix Header,路...

  • iOS 笔记

    1:在XCode6之后使用pch: 修改工程配置文件,将创建的PCH file的路径添加到TARGERS->Bui...

  • pch全局引用文件

    pch全局引用文件 Xcode6之后,pch预编译文件默认是不带的,这个需要我们去创建pch文件。

网友评论

      本文标题:Xcode pch文件配置及object - c 单例创建

      本文链接:https://www.haomeiwen.com/subject/hzdgeqtx.html