单例函数的创建 c语言,Xcode pch文件配置及object - c 单例创建

本文介绍了iOS开发中PrefixHeader(pch)文件的作用,它用于存放常用类和方法,提升编译速度。同时讲解了如何创建和配置pch文件。此外,文章还阐述了单例模式的概念,包括其优点和缺点,并通过示例展示了如何创建和在项目中使用单例。
摘要由CSDN通过智能技术生成

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

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

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

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

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

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

34851ab71f38

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

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

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

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

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

34851ab71f38

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

什么是单例

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

单例的优缺点

优点:

1.在整个程序中只会实例化一次,所以在程序如果出了问题,可以快速的定位问题所在;

2.由于在整个程序中只存在一个对象,节省了系统内存资源,提高了程序的运行效率;

缺点:

1.单例不能继承,不易扩展;

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

一个简单传值的小例子

34851ab71f38

屏幕快照 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

@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

@interface AppDelegate : UIResponder

@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

@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

@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

运行

34851ab71f38

Untitled.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值