iOS_程序执行顺序

本文深入探讨了iOS应用程序状态转换的过程,包括从活动到非活动、进入后台、回到前台以及即将终止的状态,详细解释了每个状态的方法调用及应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于这个话题其实可以变化的不多, 毕竟执行顺序是死的, 只是这个问题在面试中会被问到, 所以拿出来说一下


1.main.m 程序的入口

这里 main 函数为我们做了三件事情
a.创建了一个 UIApplication 对象;
b.指定了一个 UIApplication 的代理委托;
c.UIApplicationMain 会为我们开启个事件循环(控制我们手指触到屏幕或者其他的事件);

int main(int argc, char * argv[]) {

    @autoreleasepool {

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    }

}

2.xxx - Prefix.pch预编译头文件
3.xxx - Info.plist程序配置文件
4.InfoPlist.string 国际化文件
5.MyProject.app 最终程序
6.framework 是程序用到的框架
UIApplicationDelegate

/*UIApplicationDelegate是个协议,定义了一堆监测程序执行的方法.这些方法都有自己的触发事件;

      大致分为几类:程序启动、活跃/非活跃状态、前/后台切换、推送通知、内存警告等;*/

其中appDelegate中, 在程序还会涉及到下面这些方法,分别是在程序进行至各个状态时调用的

#pragma mark -应用程序将要取消活动状态;

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


#pragma mark -应用程序将要进入后台;

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


#pragma mark -应用程序将要进入前台;

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


#pragma mark -应用程序变为活动状态;

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


#pragma mark -应用程序将要终止(通常用在后台下载至一半是退出程序时);

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}




### C++ `std::ios_base` 用法和功能详解 #### 基本概念 `std::ios_base` 是 C++ 中流类的基础抽象基类,定义了所有 I/O 流对象共有的属性和成员函数。这些属性控制着输入输出操作的行为方式[^1]。 #### 成员类型与常量 - **成员类型** - `fmtflags`: 表示格式化标志位的枚举组合。 - `iostate`: 描述流状态的一组标志。 - `openmode`: 定义文件打开模式。 - `seekdir`: 文件指针移动方向(向前、向后等)。 - **静态公共成员变量** - `badbit`, `failbit`, `eofbit`, `goodbit`: 这些是用来检测I/O错误的状态标记。 - 对于读写权限有对应的 `app`, `binary`, `in`, `out`, `trunc`, `ate` 等选项来设置文件的操作模式[^2]. #### 关键成员函数解析 ##### 控制同步行为 为了提高程序执行速度,在不需要标准C库支持的情况下可以关闭同步机制: ```cpp // 取消同步至stdio缓冲区,默认情况下是开启的 std::ios_base::sync_with_stdio(false); ``` 另外还可以解除cin绑定到stdout的关系以进一步提升性能: ```cpp // 解除cin与stdout之间的关联 std::cin.tie(nullptr); ``` 这两句代码能够显著减少不必要的刷新动作从而加快IO处理过程. ##### 设置精度及宽度 通过调整浮点数显示的小数位数以及字段宽度实现更灵活的数据展示形式: ```cpp stream.setf(std::ios::fixed | std::ios::showpoint); // 固定小数点表示并总是显示出它 stream.precision(6); // 设定默认打印六位有效数字 stream.width(8); // 输出时至少占用八个字符的空间 ``` ##### 自定义填充字符 当指定宽度不足以容纳实际数据长度时会自动补全剩余部分为空格或其他自定义符号: ```cpp stream.fill('*'); // 使用星号(*)作为填充符代替空白 ``` ##### 获取当前参数配置 有时可能需要获取当前使用的各种设定以便后续恢复原状或者记录日志用途: ```cpp auto old_flags = stream.flags(); // 复制现有格式化标志副本 auto old_precision = stream.precision(); // 查询现行精确度等级 auto old_fill_char = stream.fill(' '); // 查看原先填充字符是什么 ``` ##### 寻找位置偏移量 对于随机访问型设备而言,允许改变内部定位器的位置来进行非顺序式的存取活动: ```cpp pos_type seekoff(off_type offset, ios_base::seekdir direction, open_mode mode=ios_base::in|ios_base::out); pos_type seekpos(pos_type position, open_mode mode=ios_base::in|ios_base::out); ``` 上述两个方法分别用于基于相对距离(`seekoff`)或是绝对地址(`seekpos`)的方式重置光标所在之处.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值