iOS 中 attribute((constructor)) 修饰的函数

开发环境声明:此文描述的 attribute((constructor)) 特指使用 Objective-C 开发 iOS、MacOS,Swift 语言不支持这种属性修饰符。

初识 attribute((constructor))

在 Objective-C 开发中,attribute((constructor)) 是一个 GCC 和 Clang 编译器特性,允许开发者在程序启动时自动执行一些函数。使用这个属性修饰的函数会在 main 函数之前执行,通常用于初始化一些全局状态或者执行一些在程序开始时就需要完成的操作。

使用 attribute((constructor))

这种属性的使用方法很简单,只需要在函数定义前加上 __attribute__((constructor)) 修饰符即可。

objective-c 复制代码
#import <Foundation/Foundation.h>

__attribute__((constructor))
void myCustomInitializer(void) {
    NSLog(@"This function is called before main");
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"Hello, World!");
    }
    return 0;
}

在上面的代码中,myCustomInitializer 函数会在 main 函数之前执行。当你运行这个程序时,你会看到控制台输出:

This function is called before main
Hello, World!

应用场景

  1. 全局状态初始化:可以用于初始化一些全局变量或状态,这些变量在程序开始时就需要使用。
  2. 日志初始化:如果有全局的日志系统,可以在程序启动时进行初始化。
  3. 注册插件或模块:在应用启动时自动注册一些插件或模块,使其在整个应用生命周期中可用。

注意事项

  1. 执行顺序 :如果有多个使用 attribute((constructor)) 修饰的函数,执行顺序是不确定的,因此不要依赖于特定的执行顺序。
  2. 执行时间 :这些函数会在 main 函数之前执行,因此会增加应用的启动时间,要谨慎使用,尽量避免进行耗时操作。
  3. Objective-C 的使用:虽然可以在 Objective-C 中使用,但要注意和 Objective-C runtime 的初始化顺序相互配合,避免在 Objective-C runtime 尚未完全初始化时进行依赖 Objective-C 特性的操作。

更复杂的例子

下面是一个更复杂的例子,展示了如何使用 attribute((constructor)) 初始化一个全局对象:

objective-c 复制代码
#import <Foundation/Foundation.h>

@interface MyGlobalManager : NSObject
+ (void)initializeManager;
@end

@implementation MyGlobalManager

+ (void)initializeManager {
    NSLog(@"MyGlobalManager is initialized");
    // 全局初始化代码
}

@end

__attribute__((constructor))
void initializeGlobalManager(void) {
    [MyGlobalManager initializeManager];
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"Application is starting");
        // 应用的其他代码
    }
    return 0;
}

运行这个程序,你会看到:

MyGlobalManager is initialized
Application is starting


What about attribute((constructor)) in Swift ?

在 Swift 中,无法直接使用 __attribute__((constructor)) 这种 GCC/Clang 特性,因为 Swift 语言不支持这种属性修饰符 (已经在开头声明 )。不过,我们可以通过其他方法实现类似的效果,例如使用 @UIApplicationMain、全局变量的初始化方法、或使用 Objective-C 的方法结合 Swift 来达到在程序启动时执行某些代码的目的。

使用 Swift 实现类似效果

1. 全局变量的初始化

Swift 中全局变量在应用启动时会初始化,可以利用这一特性来执行一些初始化代码。

swift 复制代码
import Foundation

let initialize: Void = {
    print("This code runs before main")
}()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        print("Application did finish launching")
        return true
    }
}

在这个例子中,全局变量 initialize 在应用启动时会被初始化,因此其闭包中的代码会在 main 函数之前执行。

2. 使用 Objective-C 桥接

如果需要使用 __attribute__((constructor)) 特性,可以在 Objective-C 文件中定义,然后在 Swift 中调用。

Objective-C 部分

创建一个 Objective-C 文件,例如 Initializer.m

objective-c 复制代码
// Initializer.m
#import <Foundation/Foundation.h>

__attribute__((constructor))
static void myConstructor() {
    NSLog(@"Objective-C constructor is called before main");
}

创建一个桥接头文件,例如 YourProject-Bridging-Header.h,并在其中导入 Initializer.h

objective-c 复制代码
// YourProject-Bridging-Header.h
#import "Initializer.h"

Swift 部分

确保在 Xcode 项目的设置中配置了桥接头文件,然后正常使用 Swift 编写应用:

swift 复制代码
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        print("Application did finish launching")
        return true
    }
}

运行这个项目时,控制台会输出:

Objective-C constructor is called before main
Application did finish launching

3. 使用 UIApplicationMain

在 Swift 中,@UIApplicationMain 会自动生成一个 main 函数并处理应用的启动过程,实际上你很少需要显式编写初始化代码。通过在 AppDelegate 中的 application(_:didFinishLaunchingWithOptions:) 方法中进行初始化,也能实现类似效果:

swift 复制代码
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 初始化代码
        print("Application did finish launching")
        return true
    }
}

通过 attribute((constructor)) 实现组件化

通过 __attribute__((constructor)) 实现组件化的主要思路是利用这一特性来自动注册或初始化组件,使得每个组件在应用启动时都能自动执行其初始化代码。这在模块化或插件化系统中非常有用,因为它可以自动地将组件注册到系统中,而不需要在应用启动时显式地调用每个组件的初始化代码。

下面详细说明如何通过 __attribute__((constructor)) 实现组件化,并提供一个具体的例子。

组件化的基本思路

  1. 定义一个组件协议:每个组件都需要实现这个协议。
  2. 组件注册器:一个全局的组件注册器,用来管理所有注册的组件。
  3. 使用 __attribute__((constructor)) :每个组件通过 __attribute__((constructor)) 注册到全局注册器中。

具体步骤

1. 定义组件协议

首先,定义一个组件协议(例如 Component),所有组件都需要实现这个协议:

objective-c 复制代码
// Component.h
#import <Foundation/Foundation.h>

@protocol Component <NSObject>
@required
- (void)initializeComponent;
@end
2. 组件注册器

创建一个全局的组件注册器,用来管理和调用所有注册的组件:

objective-c 复制代码
// ComponentRegistry.h
#import <Foundation/Foundation.h>

@interface ComponentRegistry : NSObject
+ (instancetype)sharedInstance;
- (void)registerComponent:(id<Component>)component;
- (void)initializeAllComponents;
@end

// ComponentRegistry.m
#import "ComponentRegistry.h"

@interface ComponentRegistry ()
@property (nonatomic, strong) NSMutableArray<id<Component>> *components;
@end

@implementation ComponentRegistry

+ (instancetype)sharedInstance {
    static ComponentRegistry *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init {
    if (self = [super init]) {
        _components = [NSMutableArray array];
    }
    return self;
}

- (void)registerComponent:(id<Component>)component {
    [self.components addObject:component];
}

- (void)initializeAllComponents {
    for (id<Component> component in self.components) {
        [component initializeComponent];
    }
}

@end
3. 定义组件并使用 __attribute__((constructor)) 进行注册

每个组件通过 __attribute__((constructor)) 将自己注册到组件注册器中:

objective-c 复制代码
// MyComponent.h
#import <Foundation/Foundation.h>
#import "Component.h"

@interface MyComponent : NSObject <Component>
@end

// MyComponent.m
#import "MyComponent.h"
#import "ComponentRegistry.h"

@implementation MyComponent

- (void)initializeComponent {
    NSLog(@"MyComponent is initialized");
}

__attribute__((constructor))
static void registerMyComponent(void) {
    MyComponent *component = [[MyComponent alloc] init];
    [[ComponentRegistry sharedInstance] registerComponent:component];
}

@end
4. 在应用启动时初始化所有组件

AppDelegateapplication:didFinishLaunchingWithOptions: 方法中,调用组件注册器的初始化方法:

objective-c 复制代码
// AppDelegate.m
#import "AppDelegate.h"
#import "ComponentRegistry.h"

@interface AppDelegate ()
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Initialize all registered components
    [[ComponentRegistry sharedInstance] initializeAllComponents];
    return YES;
}

@end

完整的示例

  1. Component.h
objective-c 复制代码
#import <Foundation/Foundation.h>

@protocol Component <NSObject>
@required
- (void)initializeComponent;
@end
  1. ComponentRegistry.h
objective-c 复制代码
#import <Foundation/Foundation.h>
#import "Component.h"

@interface ComponentRegistry : NSObject
+ (instancetype)sharedInstance;
- (void)registerComponent:(id<Component>)component;
- (void)initializeAllComponents;
@end
  1. ComponentRegistry.m
objective-c 复制代码
#import "ComponentRegistry.h"

@interface ComponentRegistry ()
@property (nonatomic, strong) NSMutableArray<id<Component>> *components;
@end

@implementation ComponentRegistry

+ (instancetype)sharedInstance {
    static ComponentRegistry *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init {
    if (self = [super init]) {
        _components = [NSMutableArray array];
    }
    return self;
}

- (void)registerComponent:(id<Component>)component {
    [self.components addObject:component];
}

- (void)initializeAllComponents {
    for (id<Component> component in self.components) {
        [component initializeComponent];
    }
}

@end
  1. MyComponent.h
objective-c 复制代码
#import <Foundation/Foundation.h>
#import "Component.h"

@interface MyComponent : NSObject <Component>
@end
  1. MyComponent.m
objective-c 复制代码
#import "MyComponent.h"
#import "ComponentRegistry.h"

@implementation MyComponent

- (void)initializeComponent {
    NSLog(@"MyComponent is initialized");
}

__attribute__((constructor))
static void registerMyComponent(void) {
    MyComponent *component = [[MyComponent alloc] init];
    [[ComponentRegistry sharedInstance] registerComponent:component];
}

@end
  1. AppDelegate.m
objective-c 复制代码
#import "AppDelegate.h"
#import "ComponentRegistry.h"

@interface AppDelegate ()
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Initialize all registered components
    [[ComponentRegistry sharedInstance] initializeAllComponents];
    return YES;
}

@end

要点总结

__attribute__((constructor)) 是一个非常有用的特性,允许开发者在程序启动时自动执行一些初始化操作。不过在使用时要注意性能影响和执行顺序,避免对应用启动时间产生负面影响。它适用于需要在应用启动时进行一些全局初始化工作的场景,但要避免在这些函数中进行耗时的操作。

虽然 Swift 本身不支持 __attribute__((constructor)),但我们可以通过全局变量初始化、使用 Objective-C 桥接以及在 AppDelegate 中进行初始化来实现类似的效果。这些方法在实际开发中都非常实用,并且可以满足绝大多数初始化需求。

通过 __attribute__((constructor)) 特性,可以在应用启动时自动注册和初始化各个组件,从而实现模块化和插件化的效果。这种方法简化了组件的初始化流程,使代码更具模块化和可扩展性。不过,在使用这种技术时,要注意性能影响和组件的初始化顺序问题,以确保应用能稳定、高效地启动。

相关推荐
依旧风轻2 小时前
精确计算应用的冷启动耗时
ios·swift·cold start
**K2 小时前
how to use Xcode
ide·macos·xcode
石兴稳3 小时前
MacOS 安装 mtr 网络检测工具
macos
云储储3 小时前
植物大战僵尸融合嫁接版 MAC 版本下载安装详细教程
游戏·macos·mac·pvz·mac游戏·植物大战僵尸融合嫁接版
2401_855299213 小时前
mac鼠标键盘共享:ShareMouse for Mac 激活版
macos
南国之风3 小时前
macOS使用Karabiner-Elements解决罗技鼠标G304连击、单击变双击的故障
macos·鼠标·罗技·g304
西西o3 小时前
mac中文件夹怎么显示.git隐藏文件
git·macos
TPBoreas3 小时前
Mac批量替换文件夹下所有文件内容
macos
Mac@分享吧4 小时前
GoLand 2024 for Mac GO语言集成开发工具环境
macos·go·go语言·goland·go语言开发工具