Object-C 1秒后执行static void __attribute__constructor entry
In Objective-C, the __attribute__((constructor)) is used to specify a function that should be called automatically before main() function.
To delay the execution of a function by 1 second, you can use the dispatch_after function from the Grand Central Dispatch (GCD) framework. Here's an example:
#import <dispatch/dispatch.h>
static void myFunction(void) {
// Code to be executed after 1 second
NSLog(@"My function executed after 1 second");
}
static void __attribute__((constructor)) entry() {
// Delay execution of myFunction by 1 second
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
myFunction();
});
}
In the above code, the myFunction is defined as the function that will be executed after 1 second. The entry function is marked with the __attribute__((constructor)) attribute to ensure that it is called automatically before main().
Inside the entry function, the dispatch_after function is used to schedule the execution of myFunction after a delay of 1 second. The dispatch_time is used to calculate the time at which the block of code should be executed. The block is then dispatched to the main queue using dispatch_get_main_queue().
When the program runs, the myFunction will be executed after a delay of 1 second
AI 优化版
原文地址:https://www.cveoy.top/t/topic/h9ud 著作权归作者所有。请勿转载和采集!